slimcontroller icon indicating copy to clipboard operation
slimcontroller copied to clipboard

Named Routes

Open Patabugen opened this issue 10 years ago • 5 comments

Is it possible to have Named Routes, which I can then use in $app->UrlFor() ?

Patabugen avatar Jul 07 '14 22:07 Patabugen

Yes,

By default routes are named with their controller:action so:

$this->app->addRoutes(array(
    '/bla' => array('get' => 'Controller:index')
));

$this->app->urlFor('Controller:index'); // -> "/bla"

yaworsw avatar Jul 08 '14 13:07 yaworsw

One way to specify the name of the routes is to do something like:

$app->addControllerRoute('/foo', 'PostsController:indexAction')->via('GET')->name('posts_path');

yaworsw avatar Jul 08 '14 13:07 yaworsw

yes adding routes name in the array would be much appreciated

mouhsinelonly avatar Aug 21 '14 16:08 mouhsinelonly

I add new controller by:

addControllerRoute('hello', 'Index:index')->via('GET')->name('name'); 

and i have problem with

Invalid argument supplied for foreach()
Location:lib/vendor/slim/slim/Slim/Router.php in handleError , line 147

exactly is:

protected function processGroups()
{
    $pattern = "";
    $middleware = array();
    foreach ($this->routeGroups => NULL

what i'm doing wrong?

pioter avatar Sep 19 '14 11:09 pioter

Hmm.

I just tried

$application->addControllerRoute('/hello', 'Home:index')->via('GET')->name('name');

And it worked fine. Could the problem be somewhere else?

yaworsw avatar Sep 20 '14 15:09 yaworsw