How to add slug and ID URL to Laravel 4 Route?

前端 未结 3 1819
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 03:08

I would like to add nice Slug URL to my Laravbel Project. I currently am using ID numbers.

My goal is to continue using Numbers but also to use Slugs for b

相关标签:
3条回答
  • 2021-02-10 03:25

    Consider using RESTful Controllers, this will generate a url based on the controller and function name:

    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:

    // 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.

    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:

    // 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!

    0 讨论(0)
  • 2021-02-10 03:27

    There is already a popular package that handle slugs and its possible issues: 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.

    0 讨论(0)
  • 2021-02-10 03:46

    I wouldn't pass two parameters to your function, just treat it like an ID in both cases. 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. 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:

    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'));
    }
    
    0 讨论(0)
提交回复
热议问题