Laravel 5.6 附加 Route::resource() 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49867423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:38:53  来源:igfitidea点击:

Laravel 5.6 additional Route::resource() Parameters

laravellaravel-5laravel-routing

提问by Ricky

I would like to know how to add additional parameters to Laravel's Route Resource without using Query Strings.

我想知道如何在不使用查询字符串的情况下向 Laravel 的路由资源添加其他参数。

I created a controller (CustomerController) with all the built-in resources and then, added the following route:

我使用所有内置资源创建了一个控制器 (CustomerController),然后添加了以下路由:

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):

我尝试了以下操作,但出现未找到异常 (NotFoundHttpException):

Route::resource('customers', 'CustomerController')->parameters([
    'index' => 'page'
]);

Is this possible? If so, how can i accomplish it?

这可能吗?如果是这样,我该如何实现?

回答by sam

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.

资源控制器必须实现一组定义的方法,然后由路由器映射到适当的 HTTP 动词和路径。这些方法、路径和动词构成了不可调整的契约的一部分,否则使用实现资源控制器的 Laravel 应用程序将是一件令人头疼的事。

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.

资源控制器擅长在所有 Laravel 应用程序中提供相同的体验,如果你的应用程序需要资源控制器不支持的行为,那么这表明你不应该使用它们,而应该手动注册你的路由。

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');

The documentation on Resource Controllerscovers exceptand only.

有关资源控制器的文档涵盖了exceptonly.

回答by UserHelpNeeding02356

Try this :

尝试这个 :

Route::resource('customers', 'CustomerController')->parameters([
    'customers' => 'page'
]);
The example above generates the following URIs for the resource's show route:

/customers/{page}

you set the name route: 'index'replace it by the variable : 'customers'name of your ressource

您设置了名称路由:'index'将其替换为变量:'customers'name of your ressource