Laravel以html格式嵌套资源路由参数&网址

时间:2014-06-16 20:02:53

标签: php laravel laravel-4

我基本上试图在嵌套资源的视图中查看是否有更有效或正确的方法来访问路由参数。下面的代码演示了我正在做的事情,从路径中捕获所有参数:/schools/1/classes/2/teachers/4/assignments 进入控制器索引方法,然后进行视图并将所有这些参数传递给它,以便在视图中我可以制作使用相同路径格式的表格和链接。参数。有没有更好的办法? Laravel Paste

//
//     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]))) }}




-

1 个答案:

答案 0 :(得分:0)

你总是需要传递参数,这很简单,但我认为如果你使用这样的关联数组会更好:

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

看起来更易读且易于理解。