带有连字符404错误的Laravel路由参数
嗨,我今天在使用laravel框架时遇到了这个问题。我想为带有连字符404错误的Laravel路由参数,laravel,parameters,routes,http-status-code-404,hyphen,Laravel,Parameters,Routes,Http Status Code 404,Hyphen,嗨,我今天在使用laravel框架时遇到了这个问题。我想为类别模型创建动态SEO链接,但遇到了路由参数问题 这是我在web.php中的路线: Route::get('transport/{category?}', 'JobController@index'); 这是路由重定向到的方法: public function index(Category $category = null) { if ($category) { $jobs = Job::whereCategor
类别
模型创建动态SEO链接,但遇到了路由参数问题
这是我在web.php
中的路线:
Route::get('transport/{category?}', 'JobController@index');
这是路由重定向到的方法:
public function index(Category $category = null)
{
if ($category) {
$jobs = Job::whereCategoryId($category->id)->orderBy('id', 'desc')->paginate(10);
} else {
$jobs = Job::orderBy('id', 'desc')->paginate(10);
}
return view('jobs')->with([
'jobs' => $jobs,
'categories' => Category::all(),
'current_category' => $category,
]);
}
类别
表如下所示:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('token');
$table->longText('description');
$table->timestamps();
});
作业
与类别
相关,包括:
$table->integer('category_id');
类似于
/transport/furniture
的路线有效,但类似于/transport/other vehicles
的路线无效,我得到一个404错误。我在开始时传递的参数是类别
模型中的名称
。然后我对代码进行了更改,添加了token
列,并开始传递它。应用程序仍然需要name
而不是token
,因此我更改了从getRouteKeyName()
返回的值,它就可以工作了
之前:
public function getRouteKeyName()
{
return 'name';
}
public function getRouteKeyName()
{
return 'token';
}
之后:
public function getRouteKeyName()
{
return 'name';
}
public function getRouteKeyName()
{
return 'token';
}
你能在这里分享分类模型吗?您是否在模型中的
getRouteKeyName
中设置了默认密钥名?您是否设置了使用slug的路由模型绑定?@Aashishgaba您救了我的命,谢谢您提供的线索。这是我的分类模型,在公共函数getRouteKeyName(){return'name';}
之前,我将其更改为公共函数getRouteKeyName(){return'token';}
并且它可以工作@KurtFriars也感谢您的回复