gpt4 book ai didi

资源方法参数中的 Laravel 路由前缀忽略

转载 作者:行者123 更新时间:2023-12-04 11:59:32 26 4
gpt4 key购买 nike

我需要在我的项目中实现本地化。
我添加了一个带有前缀的组。

Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'
], function () {

...

Route::resource('projects', 'ProjectsController');

...

})


中间件 setlocale

<?php

namespace App\Http\Middleware;

use Closure;

class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
app()->setLocale($request->segment(1));
return $next($request);
}
}

路由器列表
+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+
| | POST | {locale}/projects | projects.store | App\Http\Controllers\ProjectsController@store | web,setlocale,auth |
| | GET|HEAD | {locale}/projects | projects.index | App\Http\Controllers\ProjectsController@index | web,setlocale,auth |
| | GET|HEAD | {locale}/projects/create | projects.create | App\Http\Controllers\ProjectsController@create | web,setlocale,auth |
| | DELETE | {locale}/projects/{project} | projects.destroy | App\Http\Controllers\ProjectsController@destroy | web,setlocale,auth |
| | PUT|PATCH | {locale}/projects/{project} | projects.update | App\Http\Controllers\ProjectsController@update | web,setlocale,auth |
| | GET|HEAD | {locale}/projects/{project} | projects.show | App\Http\Controllers\ProjectsController@show | web,setlocale,auth |
| | GET|HEAD | {locale}/projects/{project}/edit | projects.edit | App\Http\Controllers\ProjectsController@edit | web,setlocale,auth |
+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+


一切正常。但是有一个问题。
当我打开路线时:

http://127.0.0.1:8000/en/projects/1 (Show project)



我收到一个错误

Argument 1 passed to App\Http\Controllers\ProjectsController::show() must be an instance of App\Project, string given



我将 $locale 添加到我的 show 方法中,并且它有效

public function show($locale, Project $project, Request $request)
{
}

如何摆脱方法中的参数以免将其插入任何地方。

我试过这个方法,但它不起作用:

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function callAction($method, $parameters)
{
unset($parameters['locale']);
return parent::callAction($method, $parameters); // TODO: Change the autogenerated stub
}
}


错误

Argument 1 passed to App\Http\Controllers\ProjectsController::show() must be an instance of App\Project, instance of Illuminate\Http\Request given

最佳答案

设置区域设置后,将其添加到您的中间件中:

$request->route()->forgetParameter('locale');
您现在可以删除 $locale来自 show()方法或使用它的任何其他方法。

关于资源方法参数中的 Laravel 路由前缀忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57242156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com