简体   繁体   中英

Laravel 4: named routes in named groups

Is there a way to make something like this?

Route::group(array('as' => 'admin', 'prefix' => 'admin', 'before' => 'admin'), function()
{
    Route::get('/', array('as' => 'home', 'uses' => 'AdminController@index'));
    Route::get('users', array('as' => 'users', 'uses' => 'AdminController@users'));
});

The goal is to do not include "admin" in all names and make links for above example like this:

URL::route('admin.home');
URL::route('admin.users');

Above example doesn't work:

Illegal offset type in unset
laravel/bootstrap/compiled.php:5053

Named group with nonamed routes inside works. Named routes in nonamed group work too. But not together.

Route::group(['prefix' => 'admin', 'before' => 'adminAuth'], function(){
    // If you do not want to repeat 'admin' in all route names, 
    // define the value here
    $r = 'admin';

    Route::get('users', ['as' => "{$r}.users", 'uses' => 'AdminController@users']);
    Route::get('/', ['as' => "{$r}.root", 'uses' => 'AdminController@index']);
});

In yout views/redirect you can use URL::action('ControllerName@method) and Laravel will know where redirect/point to...

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