Laravel URL生成不包括参数

时间:2014-11-18 16:27:33

标签: php laravel laravel-4 laravel-routing

当我有两个路由指向同一个动作时,我看到了Laravel 4的一个问题,一个路由在一个组中,另一个路由在routes.php文件中只是“松散”。

<?php     

// Routes.php
Route::group(array('domain' => '{subdomain}.domain.com'), function()
{
        Route::get('profile/{id}/{name}', 'ProfileController@index');
});

Route::get('profile/{id}/{name}', 'ProfileController@index');

// Template.blade.php
<a href="{{ URL::action('ProfileController@index', array(123, 'JimSmith')) }}">Jim Smith</a>

模板链接到:currentsubdomain.domain.com/profile/%7Bid%7D/%7Bname%7D,而不是分别交换123和JimSmith的ID和名称的预期行为。

如果我注释掉第一条路径(组内的路由),则代码按预期工作。为什么添加此附加路由会破坏URL生成?有办法解决这个问题吗?我错过了一些明显的东西吗?

P.S。对于那些想知道为什么我需要在两个地方使用这条路线的人来说,我可以选择使用URL::action('ProfileController@index' array('subdomain' => 'james', 'id' => 123, 'name' => 'JimSmith');

生成包含子域名的网址

1 个答案:

答案 0 :(得分:3)

问题是你没有路由的名称/别名,所以它默认为它遇到的第一个。

考虑这个替代路线结构:

Route::group(array('domain' => '{subdomain}.domain.com'), function() {
    Route::get('profile/{id}/{name}', [
        'as' => 'tenant.profile.index',
        'uses' => 'ProfileController@index'
    ]);
});

Route::get('profile/{id}/{name}', [
    'as' => 'profile.index',
    'uses' => 'ProfileController@index'
]);

现在你已经命名了这些路线,你可以这样做:

{{ URL::route('profile.index', [123, 'jSmith']) }}

或者:

{{ URL::route('tenant.profile.index', ['subdomain', 123, 'jSmith']) }}

作为一个额外的附加功能,您只能将此路由定义一次,然后在所有控制器方法中使用以下内容:

public function index($subdomain = null, $id, $name) { }

然后,您只需将www作为子域名传递,并在某个地方使用某些代码从特定操作中对www.domain.com域名进行折扣。

多租户(如果这确实是你所追求的)并不容易和直接,但有一些方法用于解决某些部分。我实际上正在计划编写有关它的教程,但是现在我希望这有点帮助。

相关问题