【问题标题】:Controller method not found - laravel 4找不到控制器方法 - laravel 4
【发布时间】:2014-03-09 00:53:36
【问题描述】:

我有这个消息抱怨试图运行任何控制器

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

找不到控制器方法。

我的路线文件中有此代码

Route::controller("/","HomeController");

Route::controller("users","UsersController");

我的控制器中的这段代码

 <?php

class UsersController extends BaseController
{

    protected $layout = "layouts.main";

    public function __construct()
    {
        $this->beforeFilter('csrf', array('on' => 'post'));
        $this->beforeFilter('auth', array('only' => array('getDashboard')));
    }

    public function getIndex()
    {
       return Redirect::to("users/register");
    }

    public function getRegister()
    {
        $this->layout->content = View::make('users.register');
    }

    
    public function postCreate()
    {
        $validator = Validator::make(Input::all(), User::$rules);
        if ($validator->passes()) {
            // validation has passed, save user in DB
            $user = new User;
            $user->firstname = Input::get('firstname');
            $user->lastname = Input::get('lastname');
            $user->email = Input::get('email');
            $user->password = Hash::make(Input::get('password'));
            $user->save();

            return Redirect::to('users/login')->with('message', 'Thanks for registering!');
        } else {
            return Redirect::to('users/register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
        }
    }

    function getLogin()
    {
        if (Auth::check()) return Redirect::to("users/dashboard")->with('message', 'Thanks for registering!');

        $this->layout->content = View::make("users.login");
    }

    function postSignin()
    {
        if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))) {
            return Redirect::to('users/dashboard')->with('message', 'You are now logged in!');
        } else {
            return Redirect::to('users/login')
                ->with('message', 'Your username/password combination was incorrect')
                ->withInput();
        }
    }

    public function getDashboard()
    {
        $this->layout->content = View::make("users.dashbord");
    }

    public function getLogout()
    {
        Auth::logout();
        return Redirect::to('users/login')->with('message', 'Your are now logged out!');
    }

 

我运行这个命令

php artisan routes
+--------+---------------------------------------- --------------------+------+---------- ---------+----------------+----------------+ |域名 |网址 |姓名 |行动 |过滤器前 |后过滤器 | +--------+---------------------------------------- --------------------+------+---------- ---------+----------------+----------------+ | | GET 索引/{一个?}/{两个?}/{三个?}/{四个?}/{五个?} | | HomeController@getIndex | | | | |获取 / | | HomeController@getIndex | | | | |获取 {_missing} | | HomeController@missingMethod | | | | | GET 用户/索引/{一个?}/{两个?}/{三个?}/{四个?}/{五个?} | |用户控制器@getIndex | | | | |获取用户 | |用户控制器@getIndex | | | | | GET 用户/注册/{一个?}/{两个?}/{三个?}/{四个?}/{五个?} | |用户控制器@getRegister | | | | | POST 用户/创建/{一个?}/{两个?}/{三个?}/{四个?}/{五个?} | |用户控制器@postCreate | | | | | GET users/login/{one?}/{two?}/{three?}/{four?}/{five?} | |用户控制器@getLogin | | | | | POST users/signin/{one?}/{two?}/{three?}/{four?}/{five?} | |用户控制器@postSignin | | | | | GET users/dashboard/{one?}/{two?}/{three?}/{four?}/{five?} | |用户控制器@getDashboard | | | | | GET users/logout/{one?}/{two?}/{three?}/{four?}/{five?} | |用户控制器@getLogout | | | | |获取用户/{_missing} | |用户控制器@missingMethod | | | +--------+---------------------------------------- --------------------+------+---------- ---------+----------------+----------------+

我试图访问localhost:8000/users/login 或任何控制器中的任何方法 出现这条消息

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.

【问题讨论】:

    标签: php routing laravel-4


    【解决方案1】:

    尝试改变路由注册的顺序

    Route::controller("users","UsersController");
    
    Route::controller("/","HomeController");
    

    【讨论】:

    • 这对我也有用,你介意分享一下为什么会这样吗?
    • 路由从上到下注册。如果找到任何匹配,则执行匹配的回调,Laravel 不会继续查找。回家路线"/" 应该放在最后一条,因为这表示没有更多可查找的内容。
    • 如果您使用的是 RESTful 资源控制器,请确保方法与操作匹配:GET 用于编辑,POST 用于存储等。
    猜你喜欢
    • 2016-04-04
    • 2014-05-12
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多