2014-04-20 35 views
0

我的路由和變量中存在問題。我需要將foreach循環中的變量傳遞給路由。這裏是錯誤meesage「Route [patientName]未定義」。 這裏是homepage.blade.php:Route [name]未在laravel 4.1中定義

@foreach($patientsList as $patients) 
       <tr> 
        <td>{{ $patients->name }}</td> 
        <td>{{ $patients->email }}</td> 
        <td>{{ $patients->address }}</td> 
        <td>{{ $patients->phone_number }}</td> 
        <td>{{ $patients->type }}</td> 
        <td><a href="{{ URL::route('patientName', array('pname' => $patients->name)) }}" class="view-profile">View Profile</a></td> 
       </tr> 
       @endforeach 

routes.php文件:

Route::get('patientName/{pname}','[email protected]'); 

控制器

<?php 
class PatientsController extends BaseController{ 
    /*some functions*/ 
    function getPatientName($patientName){ 
     return $patientName; 
    } 
} 

回答

0

溶液1
URL::route生成一個鏈接到一個命名的路由 - 所以它期望一個路由的名字作爲參數。在你的代碼中,你沒有指定你的路由名稱,所以URL助手不能生成鏈接並拋出未找到路由的錯誤。檢查下面

//routes.php - specify a route's name 
Route::get('patientName/{pname}', array('as'=>'patientName', 'uses'=>'[email protected]')); 

更正現在你可以放心地使用

<a href="{{ URL::route('patientName', array('pname' => $patients->name)) }}" class="view-profile">View Profile</a> 


重要:記住運行composer dump-autoload修改routes.php文件文件之後。



解決方案2
在另一方面,你可以更改URL生成方法和使用URL::to。它期望一個包含路徑的參數。

<a href="{{ URL::to('patientName/'.$patients->name) }}" class="view-profile">View Profile</a> 



結論 參考文檔基於路線
http://laravel.com/docs/routing

在安全URL生成的不同方式的細節,此外,一定要查詢的網址助手文檔的一些有用的東西
http://laravel.com/docs/helpers#urls