Should I use routes or controllers in Laravel 4?

Asked

Viewed 1,374 times

4

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

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 should I use? Routes or Controllers? Why?

3 answers

11


Generally, the tutorials you find on the internet demonstrate small projects, or just snippets of code. In these cases, as the need for organization and project patterns is low, the writer usually does all possible ways to demonstrate the existence of these possibilities.

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

Just like the many coding standards that exist you should choose which one you will adopt like, 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, he writes to the Router.

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

  • I agree with that answer. Wrong is only a matter of perspective. I think that in the route file it is acceptable for you to redirect etc, if you go beyond this, if you start to involve the logic of the application, it is better to think about using a controller (to dispatch). This way, you will even be better prepared to in the future be able to "scale" the functionality in question.

  • I also do so by putting everything in controllers separate for each page/program.

2

I’m no expert on Laravel but from what I’ve seen so far on the framework and on programming, the organization should be one of the points to be observed, 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 more lines of code in your project.

-1

Depends.

Copy and lap down the current routes.php of a project under 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: we need to maintain the organization, and we need to follow a coherent pattern - yours and/or the team.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.