简体   繁体   中英

Laravel 4 - Named Routes in RESTful Controllers

I'm trying to define named routes together with a RESTful controller. In my routes.php I have this:

Route::controller('blog', 'BlogController', array('getIndex' => 'home'));
Route::controller('login', 'LoginController');

And the method in BlogController:

public function getIndex()
{      
    return View::make('blog.home');
}

When I try to access /home I receive a NotFoundHttpException , all other routes work as expected.

Shouldn't this work? I found this third parameter for Route::controller() at this post .

The "route name" is internal.

Quoting the mentioned post : "you can pass an array of method names and their corresponding route name as the third parameter to Route::controller "...

So, your array attaches getIndex method to the home route name . But "route name" is one thing, internal to the app, but not an externally accessible URL, as you try to use it.

You may attach the URL to the named route with something like this:

Route::get('home', array('as' => 'home'));

In this case, the first "home" is the URL part, while the second "home" is the route name (which you attached to "getIndex" method). See http://laravel.com/docs/routing#named-routes

With a named route we can use an identifier for a route (for example, the name "dashboard") but later, at any moment, we can make it accessible through any URL, without the need to make a global search and replace throughout the whole application code. We can attach the "main" or "home" URL path to the "dashboard" named route... so... "the route name is internal".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM