Lance
Lance

Reputation: 4820

Laravel Controller not working

I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.

I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:

class UsersController extends BaseController 
{
    public $restful = true;

    public function action_index()
    {
        echo 'hi';
    }
}

In the routes.php file, I have

Route::get('users', 'UsersController@index');

But, when I go to

http://localhost:8888/laravel/public/users

I'm greeted with a message that says "ReflectionException Class UsersController does not exist"

enter image description here

I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering

which PHP

in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.

I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.

Thanks a bunch!

Upvotes: 1

Views: 9611

Answers (2)

msturdy
msturdy

Reputation: 10794

You need to use the Route::controller method to reference your Controller:

Route::controller('test', 'TestController');

...and rename your file (as Cryode mentions ) to be TestController.php.

Note - if you want to use the filename as test.php, then you will need to use composer to update the autoload settings.

Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method

public function action_index() {}

to be

public function getIndex() {}

the get represents a HTTP GET request... the same applies for post (HTTP POST) and any (GET or POST.. )

Upvotes: 3

Aken Roberts
Aken Roberts

Reputation: 13467

I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.

So users.php should be UsersController.php. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include() call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.

Upvotes: 2

Related Questions