Named quiet trails in Laravel 4

So, I was able to get sedative controllers working with

Route::controller('users','UserController'); class UserController extends BaseController { public function getAccount(){} } 

and therefore /users/account works. However, if I try to do something like

 Route::any('account',array('as' => 'account','uses' => ' UserController@account ')); 

and go to /account , it does not work ( NotFoundHTTPException ). Is there a way to use named routes and sed controllers? I like how the rest system interrupts requests and how named routes encapsulate URIs and separate them from function names. This worked in Laravel 3. Did I miss something in the syntax, or did Laravel 4 purposefully prohibit this mix-and-match behavior? Thanks...

+4
source share
3 answers

Try the following:

 Route::get('/',array('as'=>'named_route','uses'=>' yourRestfulController@getMethod ')); 

This works well for me. The trick was to add an action type after @part. You must use the full name of the method, unlike L3.

+1
source

This will depend entirely on how you determined the routes. If it does not work, try reordering the definitions.

But since Laravel makes things easier for you, you can pass an array of method names and their corresponding route name as the third parameter to Route::controller .

 Route::controller('users', 'UsersController', ['getProfile' => 'user.profile']); 

This may not apply directly to your situation, but it is very convenient.

+20
source

This works well for me. The trick was to add an action type after @part. You must use the full name of the method, unlike L3.

Since the prefix is ​​REST get, post, etc. are templates to distinguish which type of REST it implements. When you named the route of the sedation controllers, they no longer acted as RESTful controllers, but the normal controller that you want to name. An example of this:

 Route::get('user/profile/', array('as'=>'dashboard', 'uses'=>' ProfileController@showDashboard ')); 

Consider this: Suppose we want SystemController to be a RESTful controller, so you define:

 Route::controller('/', 'SystemController'); 

Then you want to name postDashboard on SystemController as dashboard , so you change your routes as:

 Route::get('user/profile/', array('as'=>'dashboard','uses'=>' SystemController@postDashboard ')); Route::controller('/', 'SystemController'); 

In this scenario, postDashboard should not be accessible through the GET protocol, since we declared it POST , that is, if Laravel viewed it as a RESTful Controller, since we named it that way it would be considered as normal not RESTful, so we can access it tru GET protocol. Calling it that way would be so abruptly inappropriate because we first violate what we want, and Laravel recommends considering SystemController as RESTful.

I think you should consider the Jason Lewis post a suitable answer. There are no hard feelings @arda, as you are right too.

0
source

Source: https://habr.com/ru/post/1483683/


All Articles