简体   繁体   中英

Laravel 4 routing

I am new to Laravel 4 and am having a hard time grasping routes. I have a frontend to my site and a backend. All the stuff that happens on the backend I want to have displayed under example.com/dashboard/... . I also want to use resourceful controllers. What do I need to setup in routes.php to have it so I can always refer to my users controller but have it all happen under dashboard in the URL?

Example:

I link to users/edit/1 but in the URL looks like example.com/dashboard/users/edit/1 . Dashboard should have an index page (so example.com/dashboard actually shows a page) but all other URLs are appended to that.

I think this is covered pretty well in the Larvel 4 Documentation .

Unless I'm misunderstanding, this should get you the desired results for your example case:

Route::get('dashboard', 'DashboardController@index');
Route::get('dashboard/users/edit/{id}', 'UsersController@edit');
etc.

// edit

Alternatively, using a Closure callback, you could do something like this:

Route::get('dashboard/users/{var1}/{var2?}', function($var1, $var2 = null)
{
    $controller = new UsersController;
    return $controller->{$var1}($var2);
});

Which wouldn't require you to specify each and every route. Or, as I mentioned below in comments, you could use a Resource Controller if it suits your needs.

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