Howard
Howard

Reputation: 3758

Basic routing doesn't work correctly

I'm starting Laravel and the following works fine:

Route::get('users', function()
{
    return 'Users!';
});

However this does not work:

$test = function()
{
    return 'Users!';
}

Route::get('users', $test);

Why is this?

Upvotes: 0

Views: 40

Answers (1)

Sam
Sam

Reputation: 20486

Try turning on error reporting (error_reporting(-1);) so PHP would throw a syntax error. You are missing a semicolon:

$test = function()
{
    return 'Users!';
};

The reason a semicolon is necessary is because you are setting a value to a variable. If you are just defining a function (function test() {}), you don't need the semicolon.


To actually use a defined function like function test() {}, you will need to set up Controllers in Laravel. For example:

app/controllers/TestController.php

class TestController extends BaseController
{

    public function index()
    {
        return 'Users!';
    }

}

app/routes.php

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

Note: you will need to run composer dump-autoload anytime you add a new class (i.e. TestController) to your repository.

Upvotes: 1

Related Questions