我主要是想看看是否有一种更有效或更合适的方式来访问嵌套资源的视图中的路由参数。下面的代码演示了我正在做的事情,从路由捕获所有参数:/schools/1/classes/2/teachers/4/assignments
到controller index方法中,然后创建一个视图并将所有参数传递给它这样在视图中,我就可以创建使用相同路由格式的表单和链接&参数。有没有更好的办法?Laravel粘贴
//
// app/routes.php
//------------------------------------------------------
Route::resource('schools.classes.teachers.assignments', 'AssignmentsController');
//
// app/controllers/AssignmentsController.php
//-------------------------------------------------------
public function index($school_id,$class_id,$teacher_id)
{
$routes = array($school_id,$class_id,$teacher_id);
$assignments = $this->assignment->all();
return View::make('assignments.index', compact('assignments'))
->with('routes', $routes);
}
//
// app/views/assignments/index.blade.php
// ------------------------------------------------------------
<p>{{ link_to_route('schools.classes.teachers.assignments.index', 'All Assignments', array($routes[0],$routes[1],$routes[2])) }}</p>
//
// app/views/assignments/edit.blade.php
// -------------------------------------------------------------
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes[0],$routes[1],$routes[2],$route[3]))) }}
-
你总是需要传递参数,这很简单,但我认为如果你使用这样的关联数组会更好:
$routes = compact('school_id', 'class_id', 'teacher_id');
所以它会变成:
$routes = array(
'school_id' => $school_id,
'class_id' => $class_id,
'teacher_id' => $teacher_id
);
你可以用:
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes['school_id'], ['class_id'], ['teacher_id']))) }}
看起来更容易读懂。