Laravel 重命名路由资源路径名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27806257/
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
Laravel rename routing resource path names
提问by tgezginis
Can we rename routing resource path names in Laravel like in Ruby on Rails?
我们可以像在 Ruby on Rails 中一样重命名 Laravel 中的路由资源路径名称吗?
Current
当前的
/users/create -> UsersController@create
/users/3/edit -> UsersController@edit
.. I want like this;
..我要这样;
/users/yeni -> UsersController@create
/users/3/duzenle -> UsersController@edit
I want to do this for localization.
我想这样做是为了本地化。
Example from Ruby on Rails;
来自 Ruby on Rails 的示例;
scope(path_names: { new: "ekle" }) do
resources :users
end
回答by Stretsh
I know this is an old question. I'm just posting this answer for historical purposes:
我知道这是一个老问题。我只是出于历史目的发布此答案:
Laravel now has the possibility to localize the resources. https://laravel.com/docs/5.5/controllers#restful-localizing-resource-uris
Laravel 现在可以本地化资源。https://laravel.com/docs/5.5/controllers#restful-localizing-resource-uris
Localizing Resource URIs By default, Route::resource will create resource URIs using English verbs. If you need to localize the create and edit action verbs, you may use the Route::resourceVerbs method. This may be done in the boot method of your AppServiceProvider:
use Illuminate\Support\Facades\Route; /** * Bootstrap any application services. * * @return void */ public function boot() { Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); }
Once the verbs have been customized, a resource route registration such as Route::resource('fotos', 'PhotoController') will produce the following URIs:
/fotos/crear /fotos/{foto}/editar
本地化资源 URI 默认情况下,Route::resource 将使用英语动词创建资源 URI。如果您需要本地化创建和编辑动作动词,您可以使用 Route::resourceVerbs 方法。这可以在您的 AppServiceProvider 的 boot 方法中完成:
use Illuminate\Support\Facades\Route; /** * Bootstrap any application services. * * @return void */ public function boot() { Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); }
一旦动词被自定义,资源路由注册,如 Route::resource('fotos', 'PhotoController') 将产生以下 URI:
/fotos/crear /fotos/{foto}/editar
回答by Tim Lewis
It ain't pretty, but you could define multiple routes that use the same controller function. For example:
它并不漂亮,但您可以定义多个使用相同控制器功能的路由。例如:
Route::get("user/create", "UsersController@create");
Route::get("user/yeni", "UsersController@create");
The only (glaringly obvious downside) being that you're routes will get quite cluttered quite quickly. There is a setting in app/config/app.php
where you can set/change your locale, and it could be possible to use that in conjunction with a filter to use the routes and then group those routes based on the current local/language, but that would require more research.
唯一的(明显的缺点)是你的路线很快就会变得很混乱。有一个设置app/config/app.php
,您可以在其中设置/更改您的区域设置,并且可以将其与过滤器结合使用以使用路由,然后根据当前的本地/语言对这些路由进行分组,但这需要更多的研究.
As far as I know, there isn't a way to rename resource routes on the fly, but if you get creative you can figure something out. Best of luck!
据我所知,没有办法即时重命名资源路线,但如果你有创意,你可以想出一些办法。祝你好运!
回答by Philip
You can't change the resource url's.
For this you will need to define/create each route according your needs
您无法更改资源网址。
为此,您需要根据需要定义/创建每条路线
Route::get("user/yeni", "UsersController@create");
and if you need more than one languages you can use the trans
helper function, which is an alias for the Lang::get
method
如果您需要一种以上的语言,您可以使用trans
辅助函数,它是该Lang::get
方法的别名
Route::get('user/'.trans('routes.create'), 'UsersController@create');
回答by Tim van Uum
I just had the same issue. And managed to recreate some sort of custom resource route method. It probably could be a lot better, but for now it works like a charm.
我只是有同样的问题。并设法重新创建某种自定义资源路由方法。它可能会好得多,但现在它就像一个魅力。
namespace App\Helpers;
use Illuminate\Support\Facades\App;
class RouteHelper
{
public static function NamedResourceRoute($route, $controller, $named, $except = array())
{
$routes = RouteHelper::GetDefaultResourceRoutes($route);
foreach($routes as $method => $options) {
RouteHelper::GetRoute($route, $controller, $method, $options['type'], $options['name'], $named);
}
}
public static function GetRoute($route, $controller, $method, $type, $name, $named) {
App::make('router')->$type($named.'/'.$name, ['as' => $route.'.'.$method, 'uses' => $controller.'@'.$method]);
}
public static function GetDefaultResourceRoutes($route) {
return [
'store' => [
'type' => 'post',
'name' => ''
],
'index' => [
'type' => 'get',
'name' => ''
],
'create' => [
'type' => 'get',
'name' => trans('routes.create')
],
'update' => [
'type' => 'put',
'name' => '{'.$route.'}'
],
'show' => [
'type' => 'get',
'name' => '{'.$route.'}'
],
'destroy' => [
'type' => 'delete',
'name' => '{'.$route.'}'
],
'edit' => [
'type' => 'get',
'name' => '{'.$route.'}/'.trans('routes.edit')
]
];
}
}
Use it like this in the routes.php:
在routes.php中像这样使用它:
\App\Helpers\RouteHelper::NamedResourceRoute('recipes', 'RecipeController', 'recepten');
Where the first parameter is for the named route, second the controller and third the route itself.
其中第一个参数用于命名路由,第二个参数用于控制器,第三个参数用于路由本身。
And something like this to the view/lang/{language}/route.php file:
和 view/lang/{language}/route.php 文件类似:
'edit' => 'wijzig',
'create' => 'nieuw'
This results in something like this:
这会导致如下结果:
回答by DouglasDC3
This is not possible in Laravel as they use code by convention over configuration. A resources uses the RESTfull implementation
这在 Laravel 中是不可能的,因为他们按照约定使用代码而不是配置。A 资源使用 RESTfull 实现
Therefore you have to stick to the convention of
因此,您必须遵守约定
GET /news/create
POST /news
GET /news/1
GET /news/1/edit
...