Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'uses' keyword mean in regards to laravel routing?

Tags:

php

laravel

I'm new to laravel and trying my best to RTM but having difficulty understanding a few things. I think there is an expected level on context that I am not aware of when it comes to routing. In reviewing the documentation for routing I see that the uses keyword allows one to Attach(ing) A Filter To A Controller Action, but what does that mean? I have an existing site that is using the uses keyword but I am at a loss at to what it is actually doing. Can someone explain (a tab more thoroughly than the laravel documentation does) and show a very simple example what this actually does?

like image 336
WildBill Avatar asked Nov 18 '25 22:11

WildBill


2 Answers

The keyword uses of routing is where you define which action (controller method or anonymous function) will be used to process that particular route. Take this controller method example:

Route::get('user', array('uses' => 'UserController@showProfile'));

It says that uses will call the method showProfile in your UserController class, this would be the class:

class UserController extends Controller {

    public function showProfile 
    {
       return "Hi! I'm the showProfile method!";
    }

}

So, if you hit the

http://localhost/user

You should see the message

Hi! I'm the showProfile method!

Because your route execute that action you defined in the uses.

An anonymous function (closure) example would be:

Route::get('user', array('uses' => function() {

    return "Hi, I'm a closure!";

}));
like image 126
Antonio Carlos Ribeiro Avatar answered Nov 20 '25 10:11

Antonio Carlos Ribeiro


Antonio's answer answers your (exact) question pretty perfectly, but I figured I'd add my comments to the wider context of the question, as I feel you don't actually want to know what uses is for, but instead are generally confused by the way routing works (you mention uses is for filters, which it isn't, but its use is a side-effect of adding filter configuration). So allow me to explain:

The router methods are actually kinda simple, but you just need to know what syntax they expect. There are two main forms, the unconfigured and the configured:

Unconfigured:

// syntax:
Route::get($route, $action);

// controller method
Route::get('some/route', 'Controller@action');
// closure
Route::get('some/route', function () {
    return View::make(/* etc. */);
});

But as soon as you need to add configuration to a given route (including filters, naming, etc.) you need to use a slightly different syntax:

// syntax:
Route::get($route, $config); // where $config is an array

// controller method
Route::get('some/route', array(
    'as'   => 'my.route.name',
    'uses' => 'Controller@action',
));

// closure
Route::get('some/route', array(
    'before' => 'some.filter',
    'uses'   => function () {
        return View::make(/* etc. */);
    }
));

// closure alternative
Route::get('some/route', array(
    'before' => 'some.filter',
    function () {
        return View::make(/* etc. */);
    }
));

So, as soon as you need to name your route (using as) or add a filter (using before and after) you need to turn that more simple syntax into the slightly more complex array syntax. And when you make it an array, you still need a way to tell Laravel what to actually use for the execution - that's what uses is for.

like image 20
alexrussell Avatar answered Nov 20 '25 10:11

alexrussell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!