8down votefavorite                                
3                                

Rather than using Route::getRoute::post etc for my controller requests I decided to use the Route::controller method, really helps cut down on code lines in route.php.

However I had previously set up some "route" names, for example my previous code included:

Route::get('admin/baserate/view', array('as' => 'baserateview','uses'=>'BaserateController@getView'));
                               

but now I'm using Route::controller I don't know how to implement the route alias name "baserateview". My new code looks like:

Route::controller('admin/baserate', 'BaserateController');
                               

Is there any way I can do this?

but having an alias to a route to a controller wouldn't make any sense.. which method is being invoked then if you call a route via it's alias?–reikyoushinDec 12 '13 at 14:03


I was hoping to specify that one of the methods inside the controller has a named route without having to use two lines, for example if there was an array within the Route::controller line I could utilise.                               

                       

                   

2 Answers 正确答案 此页面最底下中文显示方法



         

                      

                   
up vote22down voteaccepted                                

You can do this in the following way:

我们看到他以 . 去切割了 name ,然后加入了进去,这样我们就清楚很多啦

路由这样写 getSite 是控制器的方法

  1. Route::controller(
       'options',
       'OptionsController',

       [
           'getSite'=>'options.site'
       ]
    );

    // 现在就可以使用创建链接啦

    URL::route('options.site');记住这里,这是别名,没有使用最前面的options前缀

               

这些东西找了下 laravel 文档没找着,所以自己直接看的源码

// User Controller
Route::controller(
    'users',
    'AdminUserController',
    array(
        'getView'     => 'admin.users.view',
        'getEdit'     => 'admin.users.edit',
        'getList'     => 'admin.users.list',
        'getAdd'      => 'admin.users.add',
        'getUndelete' => 'admin.users.undelete',
        'postDelete'  => 'admin.users.delete'
    )
);
                               

  • Note, this does not work if you go to default action, e.g. index with /users (works only with /users/index) – Valdas Mar 3 '14 at 13:58                                                
  • I'm trying to do it but I'm getting the following error: Route [authpostlogin] not defined. In my routes.php I did: Route::controller('/', 'AuthController', [ 'getLogin' => 'auth.getlogin', 'postLogin' => 'authpostlogin', ]); – Anderson Silva May 20 '15 at 14:52                                                
add a comment                                
                       
                   
up vote-1down vote                                

Ok so it isn't possible to do it all on the Route:controller line. I'd have to go with both lines:

Route::controller('admin/baserate', 'BaserateController');
Route::get('admin/baserate/view', array('as' => 'baserateview','uses'=>'BaserateController@getView'));
                                   

...which works fine. I was just hoping that there would be a way to specify that one of the methods inside the controller has a named route without having to use two lines

Thanks anyway

shareimprove this answer                                        
edited Dec 12 '13 at 14:35                                            
answered Dec 12 '13 at 14:14                                            
                                               
                                           
james                                                
5721617                                                
  • 2                                                
    You should switch the order of these two lines so that the Route::get() takes precedence. If not, when you make a GET request to admin/baserate/view it will try to find it in the BaserateController. It may be working fine when you explicitly use the route alias, but not for regular requests. – Manuel Pedrera Dec 13 '13 at 8:35                                                
add a comment                                
                   

Your Answer

来自  https://stackoverflow.com/questions/20545502/laravel-4-how-to-use-the-route-name-alias-uses-with-rou...