简体   繁体   English

最终路线网址更改Laravel 5.5

[英]Final Route URL Change Laravel 5.5

I am working on a school project. 我正在做一个学校项目。 while working on a schools detail page I am facing an issue with the URL. 在学校详细信息页面上工作时,URL出现问题。 My client needs a clean URL to run AdWords. 我的客户需要一个干净的URL才能运行AdWords。 My school detail page URL: http://edlooker.com/schools/detail/4/Shiksha-Juniors-Ganapathy . 我的学校详细信息页面URL: http : //edlooker.com/schools/detail/4/Shiksha-Juniors-Ganapathy But he needs it like http://edlooker.com/Shiksha-Juniors-Ganapathy . 但是他需要像http://edlooker.com/Shiksha-Juniors-Ganapathy一样的东西。 If anyone helps me out it will be helpful, thanks in advance. 如果有人帮助我,将是有帮助的,在此先感谢。

You need to define this route after all routes in your web.php (if laravel 5.x) or in routes.php (if it is laravel 4.2). 您需要在web.php(如果是laravel 5.x)或routes.php(如果是laravel 4.2)中的所有路由之后定义此路由。

Route::get('{school}','YourController@getIndex');

And your controller should be having getIndex method like this, 而且您的控制器应该具有这样的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.
}

This way, you don't need to use the database to get URL based on the URL segment and you can directly check for the school name in the database and after fetching the data from DB, you can pass it to the school details view. 这样,您无需使用数据库即可根据URL段获取URL,并且可以直接在数据库中检查学校名称,并且从DB中获取数据后,可以将其传递到学校详细信息视图。 And it will serve your purpose. 它将满足您的目的。

Check Route Model Binding section in docs. 检查文档中的“ 路由模型绑定”部分。

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: 如果您希望模型绑定在检索给定模型类时使用id以外的数据库列,则可以在Eloquent模型上重写getRouteKeyName方法:

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

In this case, you will have to use one front controller for all requests and get data by slugs , for example: 在这种情况下,您将必须对所有请求使用一个前端控制器,并通过slug获取数据,例如:

public function show($slug)
{
     $page = Page::where('slug', $slug)->first();
     ....
}

Your route could look like this: 您的路线可能如下所示:

Route::get('{slug}', 'FrontController@show');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM