Should I use routes or controllers in Laravel 4?

4

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

I'm developing an application and I 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?

    
asked by anonymous 25.12.2013 / 16:54

3 answers

11

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

If you want to do a project of yours, I recommend that you follow a pattern that you - and your team - enjoy, organize and read and maintain without stress.

Just like the many coding standards that exist you have to choose which one to adopt, such as PSR or PECL, you can choose which form you like.

I'd rather 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.

    
25.12.2013 / 19:17
2

I am not an expert in Laravel but from what I have seen to date about the framework and about programming, the organization must be one of the points to be observed, so I have to do with myself not to mix the routes with the logic of the application. best choice even if it costs a few more lines of code in your project.

    
26.12.2013 / 16:03
-1

It depends.

Copy and paste below the current routes.php of a project in development.

<?php

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

/*** "STATIC" SITE ***/
Route::group(array(), function()
{
    Route::get('/', '[email protected]');
    Route::get('/franqueado', '[email protected]');
    Route::controller('home', 'GuestController');
});

/*** AUTHENTICATION ***/
Route::group(array('before' => 'guest'), function()
{
    Route::get( '/assinar',                '[email protected]'        );
    Route::post('/assinar',                '[email protected]' );
    Route::get( '/minha-conta',            '[email protected]'         );
    Route::post('/minha-conta',            '[email protected]'  );
    Route::get( '/franquia',               '[email protected]'         );
    Route::post('/franquia',               '[email protected]'  );
});
Route::group(array(), function()
{
    Route::any( '/minha-conta/logout',     '[email protected]'               );
    Route::any( '/confirmar/{code}',       '[email protected]'               );
    Route::get( '/solicitar-senha',        '[email protected]'       );
    Route::post( '/solicitar-senha',       '[email protected]');
    Route::any( '/recuperar-senha/{code}', '[email protected]'        )->where('code', '(.*)');
    Route::any( '/franquia/logout',        '[email protected]'               );
});

/*** "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 responses: you have to keep the organization, and you have to follow a consistent pattern - yours and / or the team.

    
05.02.2014 / 22:05