简体   繁体   English

如何向Laravel 4 Route添加slug和ID URL?

[英]How to add slug and ID URL to Laravel 4 Route?

I would like to add nice Slug URL to my Laravbel Project. 我想在我的Laravbel项目中添加漂亮的Slug URL。 I currently am using ID numbers. 我目前正在使用身份证号码。

My goal is to continue using Numbers but also to use Slugs for better SEO URL's. 我的目标是继续使用数字,但也使用Slugs更好的SEO URL。 So either a Slug or an ID will load the proper page. 因此,Slug或ID都会加载正确的页面。

Below is my current Route that uses an ID number to load a record. 下面是我当前使用ID号加载记录的Route

// View Campaign Details/Profile/Map View
Route::any("/campaign/{id}", array(
    "as"   => "campaign/{id}",
    "uses" => "CampaignController@getCampaignMap"
));

To be able to add Slug support in Laravel 4. I believe I need to add a slug database column to my campaigns table. 为了能够在Laravel 4中添加Slug支持。我相信我需要在我的campaign表中添加一个slug数据库列。

How would I go about editing my Route to work with an ID number of a slug string? 我如何编辑我的路线以使用slug字符串的ID号码?

Also since I am only wanting to use slug on a couple sections of my application, how would I do my.htaccess for this, or is an.htaccess even required? 此外,因为我只想在我的应用程序的几个部分使用slug,我将如何为此执行my.htaccess,或者甚至需要一个.htaccess?

I wouldn't pass two parameters to your function, just treat it like an ID in both cases. 我不会将两个参数传递给您的函数,只是在两种情况下都将其视为ID。 You'll need to add a "slug" column to your db if you haven't already and make sure those values are unique just like an id. 如果你还没有,你需要在数据库中添加一个“slug”列,并确保这些值与id一样唯一。 Then in your controller you can do something like this: 然后在您的控制器中,您可以执行以下操作:

public function getCampaignMap($id){
    //look for the campaign by id first
    $campaignmap  = Campaign::find($id);
    //if none is found try to find it by slug instead
    if(!$campaignmap){
        $campaignmap = Campaign::where('slug','=',$id)->firstOrFail();
    }
    return View::make('campaignmap.show', compact('campaignmap'));
}

You could also save yourself a query in some cases by checking to see if the id is numeric and then just look for it by slug if it's not numeric such as: 在某些情况下,您还可以通过检查id是否为数字来保存自己的查询,如果它不是数字,则只需通过slug查找它,例如:

public function getCampaignMap($id){
    //look for the campaign by id if it's numeric and by slug if not
    if(is_numeric($id)){
         $campaignmap  = Campaign::find($id);
    }else{
         $campaignmap = Campaign::where('slug','=',$id)->firstOrFail();
    }
    return View::make('campaignmap.show', compact('campaignmap'));
}

There is already a popular package that handle slugs and its possible issues: https://github.com/cviebrock/eloquent-sluggable 已经有一个流行的软件包来处理slug及其可能的问题: https//github.com/cviebrock/eloquent-sluggable

It can handle uniqueness adding an id suffix to previously used slugs, detect if you want to overwrite soft-deleted slugs, and some other customs configs. 它可以处理为以前使用的slugs添加id后缀的唯一性,检测是否要覆盖软删除的slugs以及其他一些海关配置。

Consider using RESTful Controllers, this will generate a url based on the controller and function name: 考虑使用RESTful控制器,这将根据控制器和函数名称生成一个url:

Route::controller('users', 'UserController');

Also, I am pretty sure resource controllers does the same thing, although I have not used them yet: 此外,我很确定资源控制器做同样的事情,虽然我还没有使用它们:

Route::resource('photo', 'PhotoController');

If you still wish to define your routes, I would add a slug column to your DB and then your route would be: 如果您仍希望定义路线,我会在您的数据库中添加一个slug列,然后您的路线将是:

// View Campaign Details/Profile/Map View
Route::any("/campaign/{slug}", array(
    "as"   => "campaign",
    "uses" => "CampaignController@getCampaignMap"
));

Keep in mind the slug would have to be unique. 请记住,slug必须是独一无二的。

In your controller: 在你的控制器中:

public function getCampaignMap($slug)
{
    $campaignmap = YourModel::where('slug', '=', $slug)->get();
}

If you wish, you may still pass the id in as well: 如果你愿意,你仍然可以传递id:

// View Campaign Details/Profile/Map View
Route::any("/campaign/{slug}/{id}", array(
    "as"   => "campaign",
    "uses" => "CampaignController@getCampaignMap"
));

Then, in your controller: 然后,在你的控制器中:

public function getCampaignMap($slug, $id)
{
    $campaignmap = YourModel::find($id);
}

Hope that helps! 希望有所帮助!

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

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