3

I'm currently trying to route as follows:

  • If user GETs /account/
    • If session has account_id, user is logged in; show his account information
    • If not, user is not logged in; show login/create form
  • If user POSTs /account/
    • If input has create, user wants to create account; create it
    • If not, user wants to login; find his account and go again to /account/

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.

1
  • I didn't tried but, this one may help you: laravel.com/api/… Commented Jan 14, 2014 at 0:47

2 Answers 2

9

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();
    }
)};
Sign up to request clarification or add additional context in comments.

1 Comment

The best way to implement this by using filter. i will try to implement an example using filter.
0

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')

2 Comments

Well, as there is no way the user will fall in 2 categories at the same time (if he gets or posts, if he is logged in or not, etc) do I really have to differentiate the actions? I didn't want to have more routes/pages, just /accounts/ for every controller method is enough
sorry, not sure exactly what you are asking. Technically, you can put the closures in the routes.php file like you were doing before, but the cleanest way to return the redirect action is the way I showed above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.