ranieri
ranieri

Reputation: 2078

Laravel 4 routing to controller method

I'm currently trying to route as follows:

My routes are set this way:

Route::get('account', function() {
    if (Session::has('account_id'))
        return 'AccountsController@show';
    else
        return 'AccountsController@index';
});

Route::post('account', function() {
    if (Input::has('create')) {
        return 'AccountsController@create';
    else    
        return 'AccountsController@login';
)};

This is somewhat how I would do with Rails, but I don't know how to point to the controller method. I just get the returned string as a result. I didn't find it in Laravel documentation (which I found really poor, or have I searched wrong?) neither in any other web tutorial.

Upvotes: 3

Views: 6718

Answers (2)

elliotanderson
elliotanderson

Reputation: 452

So, you want to put all your logic in the controllers.

You would want to do

Route::get('account', 'AccountsController@someFunction'); 
Route::get('account', 'AccountsController@anotherFunction');

Then, in the respective controllers, you would want to make your tests, then do, for example,

if (Session::has('account_id')){
    return Redirect::action('AccountsController@show');
}
else{
    return Redirect::action('AccountsController@index');
}

However, make sure you define a route for each action, so your application can detect a URL.
For example, if you want to have

 Redirect::action('AccountsController@show')

you will need to define this action like:

Route::get('/account/show', 'AccountsController@show')

Upvotes: 0

Anam
Anam

Reputation: 12169

Try the following:

Route::get('account', function() {
    if (Session::has('account_id')) {
        $action = 'show';
        return App::make('AccountsController')->$action();  
    }
    else {
        $action = 'index'; 
        return App::make('AccountsController')->$action();
    }
});

Route::post('account', function() {
    if (Input::has('create')) {
        $action = 'create';
        return App::make('AccountsController')->$action();
    }
    else {
        $action = 'login';
        return App::make('AccountsController')->$action();
    }
)};

Upvotes: 9

Related Questions