Should I use routes or controllers in Laravel 4?

I am a beginner in Laravel and I have doubts about the use of controllers, I read several examples/tutorials and see the vast majority uses the routes for example for a request of the form, to display pages, even queries to the bank.

I am developing an application and came across the following situation:

App \ routes.php

Route::group(array('as'=>'aluno', 'prefix' => 'aluno', 'before' => 'auth'), function() {
// (redireciona para o dashboard) 
Route::get('/', function()
{
    return Redirect::to('aluno/dashboard');
});

// página principal - aluno/dashboard - (app/views/aluno/dashboard.blade.php)
Route::get('dashboard', function()
{
    return View::make('aluno.dashboard');
    });
});

Controllers \ AlunoController.php

class AlunoController extends BaseController {
    public function getIndex() {
        $this->getDashboard();
    }

    public function getDashboard() {
        return View::make('aluno/dashboard');
    }
}

Both perform the same function, my question is, which one should I use? Routes or Controllers? Why?

Author: hernandev, 2013-12-25

3 answers

Generally, the tutorials you find on the internet demonstrate small projects, or just snippets of code. In such cases, as the need for organization and standards of projects is little, the writer usually does every possible way to demonstrate the existence of these possibilities.

If you want to make a project of your own, I recommend that you follow a pattern that you - and your team - like more, that you can organize and be able to read and maintain without stress.

As well as the many coding patterns that exist that you should choose which one you will adopt as, for example, the PSR or PECL, you can choose which shape you like.

I prefer to put everything in the controllers. I have a friend who if the controller method has up to 3 lines, it writes to the Router.

Again, it's up to you and your team.

 11
Author: FReNeTiC, 2013-12-25 18:17:51

I am no Expert in Laravel but from what I have seen so far about the framework and about programming, the organization should be one of the points to observe, so I have for myself that not mixing the routes with the logic of the application should be the best choice even if it costs a few lines of code more in your project.

 2
Author: henrique, 2013-12-26 15:03:50

Depends.

I copy and place below the current routes.php of a project in development.

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
*/

/*** "STATIC" SITE ***/
Route::group(array(), function()
{
    Route::get('/', 'GuestController@index');
    Route::get('/franqueado', 'GuestController@franqueado');
    Route::controller('home', 'GuestController');
});

/*** AUTHENTICATION ***/
Route::group(array('before' => 'guest'), function()
{
    Route::get( '/assinar',                'AuthController@signup'        );
    Route::post('/assinar',                'AuthController@signupHandler' );
    Route::get( '/minha-conta',            'AuthController@login'         );
    Route::post('/minha-conta',            'AuthController@loginHandler'  );
    Route::get( '/franquia',               'AuthController@login'         );
    Route::post('/franquia',               'AuthController@loginHandler'  );
});
Route::group(array(), function()
{
    Route::any( '/minha-conta/logout',     'AuthController@logout'               );
    Route::any( '/confirmar/{code}',       'AuthController@verify'               );
    Route::get( '/solicitar-senha',        'AuthController@forgotPassword'       );
    Route::post( '/solicitar-senha',       'AuthController@forgotPasswordHandler');
    Route::any( '/recuperar-senha/{code}', 'AuthController@resetPassword'        )->where('code', '(.*)');
    Route::any( '/franquia/logout',        'AuthController@logout'               );
});

/*** "LOGGED-IN USER" SITE  ***/
Route::controller('minha-conta', 'AssinanteController');
Route::controller('franquia', 'FranquiaController');

/*** SPECIAL ROUTES ***/
Route::group(array(), function()
{
    // Use these routes while in local/development environment to see
    // how these exceptional pages look like in the production environment

    Route::get('/404', function()
    {
        return Response::view('special.missing', array(), 404);
    });

    Route::get('/500', function()
    {
        return Response::view('special.error', array('error' => 'Erro desconhecido'), 500);
    });

    Route::get('/down', function()
    {
        return Response::view('special.down', array(), 503);
    });
});

As you can see, the bulk of the site is here, in these few simple lines:

/*** "LOGGED-IN USER" SITE  ***/
Route::controller('minha-conta', 'AssinanteController');
Route::controller('franquia', 'FranquiaController');

I agree with the other answers: you need to maintain the organization, and you need to follow a consistent pattern - your and/or the team.

 -1
Author: J. Bruni, 2014-02-05 21:05:32