最终路线 URL 更改 Laravel 5.5
Final Route URL Change Laravel 5.5
我正在做一个学校项目。在处理学校详细信息页面时,我遇到了 URL 的问题。我的客户需要干净的 URL 到 运行 AdWords。我的学校详细信息页面 URL:http://edlooker.com/schools/detail/4/Shiksha-Juniors-Ganapathy. But he needs it like http://edlooker.com/Shiksha-Juniors-Ganapathy。如果有人帮助我,那将很有帮助,在此先感谢。
在这种情况下,您将不得不为所有请求使用一个前端控制器并通过slugs获取数据,例如:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
....
}
您的路线可能如下所示:
Route::get('{slug}', 'FrontController@show');
您需要在 web.php(如果 laravel 5.x)或 routes.php(如果是 laravel 4.2)中的所有路由之后定义此路由).
Route::get('{school}','YourController@getIndex');
你的控制器应该有这样的 getIndex
方法,
public function getIndex($school_name)
{
print_r($school_name);die; // This is just to print on page,
//otherwise you can write your logic or code to fetch school data and pass the data array to view from here.
}
这样就不需要通过数据库根据URL段得到URL,直接在数据库中查询校名,取数据后即可从数据库中,您可以将其传递到学校详细信息视图。它将为您服务。
检查文档中的 Route Model Binding 部分。
Customizing The Key Name
If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
我正在做一个学校项目。在处理学校详细信息页面时,我遇到了 URL 的问题。我的客户需要干净的 URL 到 运行 AdWords。我的学校详细信息页面 URL:http://edlooker.com/schools/detail/4/Shiksha-Juniors-Ganapathy. But he needs it like http://edlooker.com/Shiksha-Juniors-Ganapathy。如果有人帮助我,那将很有帮助,在此先感谢。
在这种情况下,您将不得不为所有请求使用一个前端控制器并通过slugs获取数据,例如:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
....
}
您的路线可能如下所示:
Route::get('{slug}', 'FrontController@show');
您需要在 web.php(如果 laravel 5.x)或 routes.php(如果是 laravel 4.2)中的所有路由之后定义此路由).
Route::get('{school}','YourController@getIndex');
你的控制器应该有这样的 getIndex
方法,
public function getIndex($school_name)
{
print_r($school_name);die; // This is just to print on page,
//otherwise you can write your logic or code to fetch school data and pass the data array to view from here.
}
这样就不需要通过数据库根据URL段得到URL,直接在数据库中查询校名,取数据后即可从数据库中,您可以将其传递到学校详细信息视图。它将为您服务。
检查文档中的 Route Model Binding 部分。
Customizing The Key Name
If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}