dongpa6867 2013-11-25 07:46
浏览 62
已采纳

Laravel 4 Route参数仅使用给定链接上的一些参数

I understand that when you do this in Laravel:

Route::get('news/read/{year}/{month}/{date}/{title}/{id}', 'PageController@index_page');

We can use all {var} name as parameters in the controller. But how if I only want to use {id} and {title} instead of all of them in the controller?

This is currently my controller:

public function index_page($year=null, $month=date, $date=null, $title=null, $id=null) {

    $plugin_files = $this->addJqueryPlugin(array('unslider'));

    $data['css_files'] = $this->addCSS(array('styles'));
    $data['js_files'] = $this->addJS(array('main'), false);

    $data['css_plugin'] = $plugin_files['css_files'];
    $data['js_plugin'] = $plugin_files['js_files'];

    if (is_null($id)) {
        $data['title'] = 'Homepage';
        $this->layout->content = View::make('page.home', $data);
    }
    else {
        $data['isModal'] = true;
        $data['title'] = ucwords(str_replace("-", " ", $title . '--' . $id));
        $this->layout->content = View::make('page.home', $data);
    }
}

I tried putting only $title and $id but it reads from {year} and {month} instead. Only solution I can think of is change the order of the route to news/read/{title}/{id}/{year}/{month}/{date}, but I'm trying to keep the format like the previous one, is it possible?

  • 写回答

1条回答 默认 最新

  • donglan8999 2013-11-25 08:55
    关注

    Firstly, this seems wrong

    public function index_page($year=null, $month=date, $date=null, $title=null, $id=null)
    

    Remember that the order of default parameters must be as last parameters of function - check PHP Manual example here for details. I assume you misspelled $month=date for $month='some_default_date_value' ?

    Second, answering your questions, you've got at least 2 options here:

    A. routing to different methods for different parameter count or order

    //Routes
    //different routes for different params
    Route::get('news/read/{year}/{month}/{date}/{title}/{id}', 'PageController@indexFull'); 
    Route::get('news/read-id/{id}/{title}', 'PageController@indexByIdAndTitle');
    Route::get('news/read-some-more/{month}/{date}/{id}/{title}/{year}', 'PageController@indexByWeirdParamsOrder'); 
    
    
    //Controller
    //different methods for different routes
    public function indexByIdAndTitle($id, $title){ return $this->indexFull($id,$title); } 
    public function indexFull($id, $title, $year=null, $month=null, $date=null) { ... }
    public function indexByWeirdParamsOrder($month, $date, $id, $title, $year) { ... }
    

    B. changing the parameters order in route and using optional param / default value

    //Routes 
    Route::get('news/read/{id}/{title}/{year?}/{month?}/{date?}', 'PageController@indexFull'); 
    
    //Controller
    public function indexFull($id, $title, $year=null, $month=null, $date=null) { ... }
    

    Last but not least, check the Laravel docs for routing and parameters.

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

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了