How to use Router::url() to generate an URL in CakePHP 3

In CakePHP 2.x we used Router::url([]) to generate URL in any file. It works almost same with two major differennces though. The first one is that we have to call Router class manually wherever we want to use this function.

So to use Router::url() in a controller use:

use Cake\Routing\Router;

just above the XxxController class declaration. (One can use it anywhere such as in a Helper in the similar way.)

Second, we can pass query string arguments such as ?name=David as normal argument without passing ? in the params. For example:

echo Router::url([‘controller’=>’users’, ‘action’=>’forgot_password’, ‘forget_key’=>’hfgsjdfh’, ‘prefix’=>false], true);

would print full URL including hostname. In my example,


Leave a Reply