Laravel视图中的命名参数

时间:2014-01-12 12:59:00

标签: php laravel laravel-4

[底层解决方案]

我想在刀片模板中生成一个链接到我的控制器操作并传递两个参数。但是Laravel总是抛出

  

preg_replace():参数不匹配,pattern是一个字符串while   替换是一个数组

routes.php文件

Route::get('/projects/{project_id}/canals/{canal_id}/damages', array('as' => 'listDamages', 'uses' => 'DamageController@listDamages'));

DamageController.php

public function listDamages($project_id, $canal_id){
        $damages = Canal::find($canal_id)->damages;
        $canal = Canal::find($canal_id);
        $project = Project::find($project_id);
        return View::make('damages.list',array('damages' => $damages, 'canal' => $canal, 'project' => $project));
    }

view.blade.php

/* Values are:
$project->id = 71;
$canal->id = 5103;
*/

{{ Form::open(array('route' => array('listDamages',array('project_id' => $project->id,'canal_id' => $canal->id)), 'method' => 'get', 'class' =>'action-form')) }}
<button type="submit" href="{{ route('listDamages',array('project_id' => $project->id,'canal_id' => $canal->id)) }}" class="small">Auswählen</button>
{{ Form::close() }}

表单中生成的URL应如下所示:

  

/项目/ 71 /运河/ 5103 /损失

[解决方案]编辑工作代码view.blade.php

{{ Form::open(array('route' => array('listDamages', $project->id, $canal->id), 'method' => 'get', 'class' =>'action-form')) }}
<button type="submit" href="{{ route('listDamages',array($project->id, $canal->id)) }}" class="small">Auswählen</button>
{{ Form::close() }}

1 个答案:

答案 0 :(得分:1)

尝试:

echo Form::open(array('route' => array('listDamages', $project->id, $canal->id)));

虽然没有测试过以上内容。无论如何,你总是可以建立网址,虽然你失去了命名路线的好处:

echo Form::open(array('url' => 'projects/'.$project->id.'/canals/'.$canal->id.'/damages'));