在web.php中更改’resouce’名称是否有更简单的方法[Laravel 9]?

huangapple go评论107阅读模式
英文:

Is there easyest way to change 'resouce' name in web.php [Laravel9]

问题

我正在学习 Laravel9 的路由,使用示例代码,但在更改 URL 时遇到问题。
我想要将 URL 更改如下:

目前我可以访问以下 URL

127.0.0.01:8000/student/

127.0.0.01:8000/student/create

但我想要更改为这样

127.0.0.01:8000/fun_student/

127.0.0.01:8000/fun_student/create

我编写了如下代码

web.php

Route::prefix('fun_fun_student')->group(function () {
    Route::resource('students', StudentController::class);
});

但我无法显示成这个样式的 URL。
有人可以教我正确的代码吗?

更新

我尝试了

Route::resource('fun', StudentController::class);

然后我的当前路由列表如下

GET|HEAD        fun ............................. fun.index › StudentController@index  
POST            fun ............................. fun.store › StudentController@store  
GET|HEAD        fun/create .................... fun.create › StudentController@create  
GET|HEAD        fun/{fun} ......................... fun.show › StudentController@show  
PUT|PATCH       fun/{fun} ..................... fun.update › StudentController@update  
DELETE          fun/{fun} ................... fun.destroy › StudentController@destroy  
GET|HEAD        fun/{fun}/edit .................... fun.edit › StudentController@edit
英文:

I'm studying Laravel9 routing with sample code and I'm having problem changing URL.
I would like to change URL as below

Currently I can access this URL

127.0.0.01:8000/student/

and

127.0.0.01:8000/student/create

but I would like to change like this

127.0.0.01:8000/fun_student/

and

127.0.0.01:8000/fun_student/create

I wrote like this

web.php

Route::prefix('fun_fun_student')->group(function (){
    Route::resource('students', StudentController::class);   
});

but I can't display as this URL.
Could someone teach me correct code please?

UPDATE

I tried

Route::resource('fun', StudentController::class);

then my current route list

  GET|HEAD        fun ............................. fun.index › StudentController@index  
  POST            fun ............................. fun.store › StudentController@store  
  GET|HEAD        fun/create .................... fun.create › StudentController@create  
  GET|HEAD        fun/{fun} ......................... fun.show › StudentController@show  
  PUT|PATCH       fun/{fun} ..................... fun.update › StudentController@update  
  DELETE          fun/{fun} ................... fun.destroy › StudentController@destroy  
  GET|HEAD        fun/{fun}/edit .................... fun.edit › StudentController@edit 

答案1

得分: 2

更改资源的前缀,您需要更改资源名称。

示例:

Route::resource('fun_student', StudentController::class);

然后您可以使用如下路由:

示例:

<a href="{{ route('fun_student.index') }}">Students</a>

此路由应输出以下URL:

http://127.0.0.01:8000/fun_student

在更改资源之后,请记得通过运行以下命令清除缓存:

php artisan route:clear
// 或
php artisan optimize:clear

还要在项目的其他地方将 route('students.xxx') 更改为 route('fun_student.xxx')

我建议您阅读Laravel文档以获取更多信息。

英文:

To change the prefix of your resource. you have to change the resource name.

Ex:

Route::resource(&#39;fun_student&#39;, StudentController::class); 

Then you can use routes like this:

Ex:

&lt;a href=&quot;{{ route(&#39;fun_student.index&#39;) }}&quot;&gt;Students&lt;/a&gt;

This route should output the following URL:

http://127.0.0.01:8000/fun_student

After the resource changed, remember to clear cache by running:

php artisan route:clear
// or
php artisan optimize:clear

Also change route(&#39;students.xxx&#39;) to route(&#39;fun_student.xxx&#39;) in other places in your project.

I would suggest you go through the Laravel documentation for more information.

答案2

得分: 0

To complement the answer of Dasun Tharanga, if you are using route model binding, such as:

/**
 * Display the specified resource.
 */
public function show(Student $student)
{
   //...
}

You have to change the name of the argument to:

public function show(Student $fun_student)
{
   //...
}

Or you have to name resource route parameters

Route::resource('fun_student', StudentController::class)->parameters([
    'fun_student' => 'student'
]);
英文:

To complement the answer of Dasun Tharanga, if you are using route model binding, such as:

/**
 * Display the specified resource.
 */
public function show(Student $student)
{
   //...
}

You have to change the name of the argument to:

 public function show(Student $fun_student)
 {
      //...
  }

Or you have to name resource route parameters

Route::resource(&#39;fun_student&#39;, StudentController::class)-&gt;parameters([
    &#39;fun_student&#39; =&gt; &#39;student&#39;
]);

huangapple
  • 本文由 发表于 2023年4月11日 16:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定