Вопрос

So, I've been able to get restful controllers working with

Route::controller('users','UserController');

class UserController extends BaseController {
    public function getAccount(){}
}

and so /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 doesn't work (NotFoundHTTPException). Is there a way to use named routes and restful controllers in conjunction? I like how the restful system breaks up requests, and how named routes encapsulate the URI's and decouple them from the function names. This worked in Laravel 3. Am I missing something in the syntax, or did Laravel 4 purposefully disallow this kind of mix-and-match behavior? Thanks...

Это было полезно?

Решение 2

Try this:

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

This works nice for me. The trick was adding the action type after @ part. You should use the full name of the method unlike in L3.

Другие советы

This would depend entirely on the order you have defined the routes. If it's not working try reversing the order of the definitions.

But because Laravel is all about making your life easier 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 might not directly apply to your situation but it is super handy.

This works nice for me. The trick was adding the action type after @ part. You should use the full name of the method unlike in L3.

Because REST prefix get, post, and so on are patterns to distinguish what type of REST it implements. When you named restful controllers route they didn't act like RESTful controllers anymore but a normal Controller you wish to named. Example of this:

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

Consider this one: Assuming we want SystemController to be a RESTful controller so you'll define:

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

Then you want to named the postDashboard on the SystemController as dashboard, so you'll modified your routes as:

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

On that scenario,postDashboard should not be access via GET protocol since we declared it to bePOST, that is if Laravel treated it as RESTful Controller, since we named it that way it will be treated as normal not RESTful, so we can access it tru GET protocol. Naming it that way will be so dramatically not appropriate, coz we are breaking what we want first which is telling Laravel to treat SystemController as a RESTful.

I think you have to consider the post of Jason Lewis as the appropriate answer. No hard feelings @arda, since you are also correct.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top