0
編輯: 我使用的是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]'));
這是否必須處理路線如何被首先接收,如果是的話,爲什麼會發生這種情況?
Laravel將返回匹配的第一條路線。這就是爲什麼你應該從最多到最不特定的順序排列你的路線。 authors2也在authors2/new中匹配。但是因爲author2/new比authors2更具體,它不會匹配authors2。 – Barry127 2014-10-16 18:59:45
謝謝@ Barry_127 - 現在對我來說很有意義! – 2014-10-16 19:52:48