英文:
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('fun_student', StudentController::class);
Then you can use routes like this:
Ex:
<a href="{{ route('fun_student.index') }}">Students</a>
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('students.xxx')
to route('fun_student.xxx')
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('fun_student', StudentController::class)->parameters([
'fun_student' => 'student'
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论