I trying to make this REST design but I am having trouble.
I have a resource called list
I want before /list
it to have a dynamic parameter like so {username}/list
and the have the CRUD options like edit {username}/list/{id}/edit
and so on.
欢迎各位兄弟 发布技术文章
这里的技术是共享的
I would like to know how to add additional parameters to Laravel's Route Resource without using Query Strings.
I created a controller (CustomerController) with all the built-in resources and then, added the following route:
Route::resource('customers', 'CustomerController');
What i would like to do is add additional parameters to some of the default resources without creating custom routes or using query strings. For example:
Default resource with optional parameter (index):
public function index($page = 0)
{
//...
}
Desired URL:
http://www.example.com/customers
http://www.example.com/customers/{page}
I tried the following, but i get a not found exception (NotFoundHttpException):
Route::resource('customers', 'CustomerController')->parameters([
'index' => 'page'
]);
Is this possible? If so, how can i accomplish it?
Resource Controllers must implement a defined set of methods which are then mapped to the appropriate HTTP verb and path by the router. These methods, paths and verbs form part of a contract that cannot be adjusted, otherwise working with a Laravel application that implements Resource Controllers would be a headache.
Resource Controllers excel in providing the same experience across all Laravel applications, if your application requires behaviour that isn't supported out of the box by Resource Controllers then it is a sign that you should not be using them and should instead register your routes manually.
If you have just one route that needs to implement custom behaviour then you can register somemethods instead of all and then choose to register a custom route to your Resource Controllers method, something like:
Route::resource('customers', 'CustomerController')->except([
'index'
]);
Route::get('/customers/{page?}', 'CustomerController@index');
Route::controller 好像 不行 (下面的我自己亲自做的,但是应该不行) (就是对index 不管用,只有把 controller/index 使用起来才行,下面的配置就不需要了)
Route::get('common-user/order/{order_state?}','CommonUser\OrderController@getIndex')->where('order_state','[0-9]+');
Route::controller('common-user/order','CommonUser\OrderController');
Route::get('common-user/{order_state?}','CommonUser\OrderController@getIndex')->where('order_state','[0-9]+');
Route::controller('common-user','CommonUser\OrderController');
The documentation on Resource Controllers covers except
and only
.
大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦大厦
I trying to make this REST design but I am having trouble.
I have a resource called list
I want before /list
it to have a dynamic parameter like so {username}/list
and the have the CRUD options like edit {username}/list/{id}/edit
and so on.
Using the ::resource
method you can dictate the hierarchical route pattern by using a .
between your models, for instance:
Route::resource('user.list', 'ListController');
This will produce something like this:
/user/{user}/list/{list}
However you want to have a username - which is fine, we just need to add some explicit model binding in your App\Providers\RouteServiceProvider
Route::bind('user', function($value)(){
if(is_numeric($value)) {
return User::find($value);
} else {
return User::where('username', $value)->first();
}
});
Adding this explicit model binding
will attempt to resolve the user over dependency injection
first by ID
if the $value is numeric. If not, then it will attempt to match the value against the username column on the user table.
Hopefully this helps.
来自 https://stackoverflow.com/questions/46943393/laravel-5-override-route-resource-parameters
Would it be possible to attach an optional parameter in front of a route resource? I know you can do it manually by defining the routes but would be much cleaner to be able to just attach before the resource. Something like this: Route::group(array('prefix' => '/api/v2'), function() {
Route::resource('{site?}/data', 'DataController')->where('site', '[0-9]+');
}); |
No you can't have an optional pattern followed by required patterns. |
I tried to do something related to this, and I know that optional variables that aren't at the end of the URL are compiled automatically into non-optional ones by the Symfony Route class regex compiler, so this is not easily done for any route types. |
Can you have an optional parameter with out having the required pattern? |
Not for |
How about for route groups? Route::group(array('prefix' => 'foo/{foo_id?}'), function() {
Route::any('bar', 'ApiFooController@bar');
}); |
Route prefixes do not support parameters. |
resources
work in Laravel. You can exclude the routes you want to override and define them manually. Remember, Laravel is opinionated, but you can change it's opinion whenever you please. – Ohgodwhy Apr 16 at 23:12