Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller method not found - laravel 4

I have this message whine trying to run any controller

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.

I have this Code in my Route file

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

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

and this code in my Controller

 <?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!');
    }

 

Whine I run this Command

php artisan routes
+--------+------------------------------------------------------------+------+-------------------------------+----------------+---------------+
| Domain | URI                                                        | Name | Action                        | Before Filters | After Filters |
+--------+------------------------------------------------------------+------+-------------------------------+----------------+---------------+
|        | GET index/{one?}/{two?}/{three?}/{four?}/{five?}           |      | HomeController@getIndex       |                |               |
|        | GET /                                                      |      | HomeController@getIndex       |                |               |
|        | GET {_missing}                                             |      | HomeController@missingMethod  |                |               |
|        | GET users/index/{one?}/{two?}/{three?}/{four?}/{five?}     |      | UsersController@getIndex      |                |               |
|        | GET users                                                  |      | UsersController@getIndex      |                |               |
|        | GET users/register/{one?}/{two?}/{three?}/{four?}/{five?}  |      | UsersController@getRegister   |                |               |
|        | POST users/create/{one?}/{two?}/{three?}/{four?}/{five?}   |      | UsersController@postCreate    |                |               |
|        | GET users/login/{one?}/{two?}/{three?}/{four?}/{five?}     |      | UsersController@getLogin      |                |               |
|        | POST users/signin/{one?}/{two?}/{three?}/{four?}/{five?}   |      | UsersController@postSignin    |                |               |
|        | GET users/dashboard/{one?}/{two?}/{three?}/{four?}/{five?} |      | UsersController@getDashboard  |                |               |
|        | GET users/logout/{one?}/{two?}/{three?}/{four?}/{five?}    |      | UsersController@getLogout     |                |               |
|        | GET users/{_missing}                                       |      | UsersController@missingMethod |                |               |
+--------+------------------------------------------------------------+------+-------------------------------+----------------+---------------+

whine i trying to access to localhost:8000/users/login or any method in any controller this message appear

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.
like image 388
Ahmed Avatar asked Feb 11 '14 09:02

Ahmed


People also ask

How to declare Controller in Laravel?

Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.

How to Controller in Laravel?

In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory. We can create a controller using 'make:controller' Artisan command.

How to Route to Controller in Laravel?

You can define a route to this controller action like so: Route::get('user/{id}', 'UserController@show'); Now, when a request matches the specified route URI, the show method on the UserController class will be executed. Of course, the route parameters will also be passed to the method.

Where in php Controller?

Controllers are typically stored in the app/controllers directory, and this directory is registered in the classmap option of your composer. json file by default.


1 Answers

Try to change order of route registration

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

Route::controller("/","HomeController");
like image 181
Andreyco Avatar answered Oct 02 '22 23:10

Andreyco