Laravel 4 PHP:路由订购问题
问题描述:
编辑: 我使用的是Laravel 4 PHP,最初我以为这个问题与网页上的'link_to_route'方法有关,因此您只需链接到另一个不包含任何动态的网页数据。Laravel 4 PHP:路由订购问题
但是,我发现您列出路线的顺序将最终确定您的路线是否成功到达。
Authors2.php(控制器)
class Authors2_Controller extends BaseController {
public $restful = true;
public function contact() {
return View::make('authors2.index')
->with('title','Authors and Books')
->with('authors2', Author2::orderBy('name')->get());
}
public function getnew() {
return View::make('authors2.new')
->with('title', 'Add New Author');
}
routes.php文件
Route::get('authors2', array('as'=>'authors2', 'uses' =>'[email protected]'));
Route::get('authors2/new', array('as'=>'new_author', 'uses'=>'[email protected]'));
index.blade.php(视图)
@extends('layouts.default')
@section('content')
<h1>Authors2 Home Page </h1>
<ul>
@foreach($authors2 as $author2)
<li>{{ link_to_route('author2', $author2->name, $parameters = array($author2->id)) }}</li>
@endforeach
</ul>
<p>{{ link_to_route('new_author', 'Add Author') }} </p>
@endsection
当我点击“添加作者”链接时,出现错误“尝试获取非对象属性”错误。
编辑: 所以现在当我改变路由的顺序在那里,如果我列出了“authors2 /新”路线“authors2”路线之前,路线将实际工作:
Route::get('authors2/new', array('as'=>'new_author', 'uses'=>'[email protected]'));
Route::get('authors2', array('as'=>'authors2', 'uses' =>'[email protected]'));
这是否必须处理路线如何被首先接收,如果是的话,为什么会发生这种情况?
答
正如@ Barry_127所提到的,Laravel将匹配从最具体到最不具体的路线。所以它的好习惯是按照这个顺序列出你的路线。
Laravel将返回匹配的第一条路线。这就是为什么你应该从最多到最不特定的顺序排列你的路线。 authors2也在authors2/new中匹配。但是因为author2/new比authors2更具体,它不会匹配authors2。 – Barry127 2014-10-16 18:59:45
谢谢@ Barry_127 - 现在对我来说很有意义! – 2014-10-16 19:52:48