【问题标题】:Laravel dynamic route with dynamic prefix带有动态前缀的 Laravel 动态路由
【发布时间】:2015-01-21 10:31:33
【问题描述】:

我想使用以下方法在每个组路由之前添加 customer_id。 customer_id 设置为 Session::get('customer.id')。

Route::group(['prefix' => 'customer/{id}'], function($id) {
        Route::get('reports/default', array('as' => 'customer_reports_path', 'uses' => 'ReportController@getDefault'))->before('customer'); 
        Route::get('data/objects/{$object_id}', array('as' => 'customer_reports_object', 'uses' => 'DataController@getObject'));
});

第一条路线按方面工作,但是,我不知道如何正确使用第二条路线。

{{ HTML::link(route('customer_reports_object', [Session::get('customer.id'), $object_id], 'Object name') }}

链接仍然以 404 结尾。

【问题讨论】:

  • 您在该代码上的语法有点不对劲。试试这个:{{ link_to_route('customer_reports_object', 'Object name', [Session::get('customer.id'), $object_id]) }}

标签: php laravel routes prefix


【解决方案1】:

@MichaelColeman 是正确的$ 标志不允许在路由参数中。原因如下:

路由参数由正则表达式找到,仅匹配\w(单词),不包括$

Illuminate\Routing\Route@compileRoute

$uri = preg_replace('/\{(\w+?)\?\}/', '{$1}', $this->uri);

解决方案显然是删除$(这可能首先是一个错字)

Route::get('data/objects/{object_id}'...

并正确生成您的链接。 (我也建议你使用link_to_route函数)

{{ link_to_route('customer_reports_object', 'Object name', [Session::get('customer.id'), $object_id]) }}

【讨论】:

  • 酷,可能还值得注意 - link_to_route 函数实际上使用 literal 语法“link_to_route” - 否则您之前的评论(无论如何对我来说)就像@987654332 @。即 `link_to_route 是一个 laravel 辅助函数 laravel.com/docs/4.2/helpers>
【解决方案2】:

尝试在参数中不使用$,即

Route::get('data/objects/{object_id}', array('as' => 'customer_reports_object', 'uses' => 'DataController@getObject'));

【讨论】:

  • 太好了,您能否发布您需要在 HTML 中使用的实际(更正)代码以使链接正常工作,我很想看看您使用了什么
  • 别担心,我明白@lukasgeiter 的意思了
猜你喜欢
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2022-11-05
  • 2017-10-28
  • 1970-01-01
相关资源
最近更新 更多