【问题标题】:Wildcard routes in Laravel?Laravel 中的通配符路线?
【发布时间】:2014-02-15 02:34:53
【问题描述】:

有没有办法拥有通配符路线?但只有特定的名称。

例如。

我有许多通往同一个地方的路线:

/archive/gallery/1/picture/1
/masters/gallery/1/picture/1
/browse/gallery/1/picture/1

这些都加载了同一张图片,但如果我能做这样的事情那就太好了:

Route::get('{???}/gallery/{galleryId}/picture/{pictureId}', array(
    'as'=>'picture',
    'uses'=>'PictureController@getPicture'
));

但只能使用存档或母版或浏览作为通配符。

【问题讨论】:

  • 所以你有三个资源,但你想用通配符使路由器通用?

标签: laravel laravel-4


【解决方案1】:

您不能定义不同的控制器,具体取决于通配符。您必须在控制器中定义它。

Route::get('{page}/gallery/{galleryId}/picture/{pictureId}', array(
    'as'=>'picture',
    'uses'=>'PictureController@getPicture'
));

public function getPicture($page)
{
   if ($page == "archive")
        return View::make('archive');
   else if ($page == "browse")
        return View::make('browse');
   else if ($page == "masters")
        return View::make('masters');
}

请务必将路由放在路由文件的底部,否则它将覆盖其他路由:) 因为 laravel 使用先进 -> 优先 -> 出

【讨论】:

    【解决方案2】:

    你可以试试这个

    Route::get('{type}/gallery/{galleryId}/picture/{pictureId}', array(
        'as'=>'picture',
        'uses'=>'PictureController@getPicture'
    ))->where('type', 'masters|browse|archive');
    

    图片控制器:

    public function getPicture($type, $galleryId, $pictureId)
    {
        // $type could be only masters or browse or archive
        // otherwise requested route won't match
    
        // If you want to load view depending on type (using type)
        return View::make($type);
    }
    

    【讨论】:

      【解决方案3】:

      {???} 可以是一个正则表达式。

      可能是这样的{(archive|browse|masters)}

      更新:我认为上述方法适用于 L3,但 L4 必须以不同方式完成

      Route::get('/{variable}', function()
      {
          return View::make('view');
      })->where('masters', 'browse', 'archive');
      

      【讨论】:

        猜你喜欢
        • 2019-12-09
        • 2017-07-17
        • 1970-01-01
        • 2012-10-29
        • 2014-12-01
        • 2015-10-12
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多