2015-01-21 168 views
0

我想用以下方法在每个组路由前添加customer_id。 customer_id被设置为Session :: get('customer.id')。带动态前缀的Laravel动态路由

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

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

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

链接仍然在参数结束在404

+1

你在那段代码上的语法有点偏离。试试这个:'{{link_to_route('customer_reports_object','Object name',[Session :: get('customer.id'),$ object_id])}}' – lukasgeiter 2015-01-21 10:36:37

回答

2

@MichaelColeman是正确$迹象不允许在路由参数。这是为什么:

路由参数是由正则表达式找到的,它只匹配\w(字),并且不包括$

Illuminate\Routing\[email protected]

$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]) }} 
+1

很酷,或许值得注意的是,link_to_route函数实际上使用了* literal *语法的“link_to_route”,否则你以前的评论(无论如何都是这样)读取为'{{insert_your_route_here('custome ....)}}''。即'link_to_route是laravel帮助函数 2015-01-21 11:04:20

1

尝试没有$

Route::get('data/objects/{object_id}', array('as' => 'customer_reports_object', 'uses' => '[email protected]')); 
+0

这与lukasgeiter的加入一起工作! – wiesson 2015-01-21 10:43:56

+0

太棒了,你可以发布你需要在你的HTML中使用的实际(修正)代码,以获得链接工作请,我会有兴趣看看你用什么 – 2015-01-21 10:49:32

+0

别担心,我现在看什么@lukasgeiter现在意味着 – 2015-01-21 11:07:03