Laravel重命名路由资源路径名 [英] Laravel rename routing resource path names

查看:365
本文介绍了Laravel重命名路由资源路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以像在Ruby on Rails中一样在Laravel中重命名路由资源路径名吗?

Can we rename routing resource path names in Laravel like in Ruby on Rails?

当前

/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.

Ruby on Rails中的示例;

Example from Ruby on Rails;

scope(path_names: { new: "ekle" }) do
  resources :users
end

推荐答案

我知道这是一个老问题.我只是出于历史目的发布此答案:

I know this is an old question. I'm just posting this answer for historical purposes:

Laravel现在可以本地化资源. https://laravel.com/docs/5.5/controllers#restful-localizing -resource-uris

Laravel now has the possibility to localize the resources. https://laravel.com/docs/5.5/controllers#restful-localizing-resource-uris

本地化资源URI默认情况下,Route :: resource将创建 使用英语动词的资源URI.如果需要本地化创建 和编辑动作动词,您可以使用Route :: resourceVerbs方法. 这可以在您的AppServiceProvider的启动方法中完成:

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',
    ]); } 

自定义动词后,资源路由注册,例如Route :: resource('fotos','PhotoController') 产生以下URI:

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

这篇关于Laravel重命名路由资源路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆