【问题标题】:Call to undefined method - Laravel调用未定义的方法 - Laravel
【发布时间】:2013-10-23 14:17:07
【问题描述】:

我是 Laravel 新手,我正在使用 Laravel 4,我正在尝试创建一个用户控制器,如下所示:

class UserController extends BaseController {

    public function getIndex (){
    }

    public function showProfile($id){

        echo "$id";

        View::make('user.profile');
    }


    public function getNew(){
    }

    public function postNew(){

    }

    public function getLogin(){
    }

在我想使用的路线中: Route::controller('user', 'UserController');

它在所有期望 (showProfile) 中都能正常工作,例如当我转到 ../user/profile/2 时 我得到了对未定义方法的调用。

注意: 我可以使用

Route::get('user', 'UserController@getIndex');
Route::get('user/new', 'UserController@getNew');
Route::post('user/new', 'UserController@postNew');
Route::get('user/login', 'UserController@getLogin');
Route::get('user/{id}', 'UserController@showProfile');

它会正常工作,但我认为这不是一个好习惯

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    没有为您定义的路线

     ../user/profile/2
    

    您是否尝试过类似的方法:

    Route::get('user/profile/{id}', 'UserController@showProfile');
    

    【讨论】:

    • 是的,我试过了,但是它覆盖了所有其他方法,比如用户/登录,但我添加了一些使它工作的东西 Route::get('user/{id}', 'UserController@showProfile') ->where('id', '[0-9]+');所以它不会覆盖其他方法
    • 但这仍然不理想,如果我想传递 {username} 而不是 {id} 怎么办,那么我不能使用 where('id', '[0-9]+' );
    • user/profile/{id}如何覆盖其他路由?其他路线中不包含“个人资料”
    • 是的,很抱歉它适用于 /user/profile/{id} 或 {username},但我实际上正在寻找类似 ../user/{username} 或更像 Facebook ../ {用户名}
    • 在这种情况下,您需要使用路由过滤器来检查 $username 是否是存储在数据库中的实际用户名。 laravel.com/docs/routing#route-filters
    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 2016-06-05
    • 2016-09-03
    • 2015-06-10
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多