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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?