douliaodan2738 2014-02-03 12:34
浏览 29
已采纳

Laravel友好的网址,我做得对吗?

Here's my route :

Route::get('location/{id}/{title?}', array('as' => 'scouting_single', 'uses' => 'ScoutingController@get_single')); 

The class is simple:

public function get_single($id, $title ='') { 
    $location = new Location; 
    if(is_numeric($id)) { 
        $location = $location->find($id);
        if(isset($location)) {
            $author = User::find($location->author);                
            $meta = $location->find($id)->metakeywords;

            if($title == '') { 
                $slug = Str::slug($location->title); 
                return Redirect::to('location/'.$id.'/'.$slug);
            }


            return View::make('scoutingviews.view')->with('pagetitle', 'Location view')
                ->with('location', Location::find($id))
                ->with('author', $author)
                ->with('meta', $meta);  
        } else { 
            return Redirect::to('/')->with('message', 'That record is not available'); 
        }               
    } else { 
        return Redirect::to('404');
    }
}

Everything seems to work fine but after searching around it seems that others are doing it differently like saving the slugs to db, but I just want to include the id to the url... and make it optional to include the title slug. If the user removes the slug, it will redirect the user with the slug anyways.

I'm still learning laravel so please forgive the newbie question with regards to seo-friendliness, I just don't want /{id}/ and /{id}/{title} to count as duplicates

  • 写回答

1条回答 默认 最新

  • dsfsfdsf4544 2014-02-03 12:59
    关注

    If you are worried about SEO, then having a URL slug in the database is the way to go. Having the ID in the URL isn't very helpful to search engines or users. This should also simplify your code as well. In addition to that, you should take a look at Eloquent relationships where you can attach your users to locations. Instead of having

    $author = User::find($location->author);
    

    You could have

    $author = $location->author;
    

    The relationship would be a User hasMany Locations, and Location belongsTo User. This is also assuming that users can have many locations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab