Luke Snowden
Luke Snowden

Reputation: 4196

Laravel - using controllers instead of routes for actions

I've literally downloaded Laravel today and like the looks of things but i'm struggeling on 2 things.

1) I like the controllers' actions method of analysing urls instead of using routes, it seems to keep everything together more cleanly, but lets say I want to go to

/account/account-year/

how can I write an action function for this? i.e.

function action_account-year()...

is obviously not valid syntax.

2) If i had

function action_account_year( $year, $month ) { ...

and visited

/account/account_year/

An error would be displayed about missing arguments, how do you go about making this user friendly/load diff page/display an error??

Upvotes: 0

Views: 3781

Answers (3)

Suresh Bala
Suresh Bala

Reputation: 2362

You can think of the following possibility as well

class AccountController extends BaseController {

    public function getIndex()
    {
        //
    }

    public function getAccountYear()
    {
        //
    }

}

Now simply define a RESTful controller in your routes file in the following manner

Route::controller('account', 'AccountController');

Visiting 'account/account-year' will automatically route to the action getAccountYear

Upvotes: 4

Luke Snowden
Luke Snowden

Reputation: 4196

I thought I'd add this as an answer in case anyone else is looking for it:

1)

public function action_account_year($name = false, $place = false ) { 
     if( ... ) { 
             return View::make('page.error' ); 
     }
}

2)

not a solid solutions yet:

laravel/routing/controller.php, method "response"

public function response($method, $parameters = array())
{
    // The developer may mark the controller as being "RESTful" which
    // indicates that the controller actions are prefixed with the
    // HTTP verb they respond to rather than the word "action".

    $method = preg_replace( "#\-+#", "_", $method );            

    if ($this->restful)
    {
        $action = strtolower(Request::method()).'_'.$method;
    }
    else
    {
        $action = "action_{$method}";
    }

    $response = call_user_func_array(array($this, $action), $parameters);

    // If the controller has specified a layout view the response
    // returned by the controller method will be bound to that
    // view and the layout will be considered the response.
    if (is_null($response) and ! is_null($this->layout))
    {
        $response = $this->layout;
    }

    return $response;
}

Upvotes: 0

William Cahill-Manley
William Cahill-Manley

Reputation: 2405

You would have to manually route the hyphenated version, e.g.

Route::get('account/account-year', 'account@account_year');

Regarding the parameters, it depends on how you are routing. You must accept the parameters in the route. If you are using full controller routing (e.g. Route::controller('account')) then the method will be passed parameters automatically.

If you are manually routing, you have to capture the params,

Route::get('account/account-year/(:num)/(:num)', 'account@account_year');

So visiting /account/account-year/1/2 would do ->account_year(1, 2)

Hope this helps.

Upvotes: 8

Related Questions