【问题标题】:Route [name] not defined in laravel 4.1路线 [名称] 未在 laravel 4.1 中定义
【发布时间】:2014-07-08 14:52:24
【问题描述】:

我的路线和变量有问题。我需要将 foreach 循环中的变量传递给路由。这是错误消息“Route [patientName] not defined”。 这是homepage.blade.php:

@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}','PatientsController@getPatientName');

控制器

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

【问题讨论】:

    标签: php variables laravel-4 laravel-routing


    【解决方案1】:

    解决方案 1
    URL::route 生成一个指向命名路由的链接——因此它需要一个路由的名称作为参数。在您的代码中,您没有指定路由的名称,因此 URL 帮助程序无法生成链接并引发找不到路由错误。检查以下更正

    //routes.php - specify a route's name 
    Route::get('patientName/{pname}', array('as'=>'patientName', 'uses'=>'PatientsController@getPatientName'));
    

    现在你可以放心使用了

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


    重要提示:修改 routes.php 文件后记得运行composer dump-autoload



    解决方案 2
    另一方面,您可以更改 url 生成方法并使用URL::to。它需要一个包含路径的参数。

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



    结论 有关基于路由的不同安全 url 生成方式的详细信息,请参阅文档
    http://laravel.com/docs/routing

    还请务必查看 URL 帮助文档以获取一些有用的内容
    http://laravel.com/docs/helpers#urls

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 2018-10-15
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2020-05-03
      • 2016-04-02
      相关资源
      最近更新 更多