We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Group routing & filters implementation

I came from Laravel, and I wonder to know whether Phalcon php also implement group routing and filter like in Laravel 4,

below a sample routing in Laravel:

// Router for guest
Route::group(array('before' => 'guest'), function()
{
    Route::get('/', array('as' => 'homepage', 'uses' => '[email protected]'));

    Route::get('/login', array('as' => 'login', 'uses' => '[email protected]'));
    Route::post('/login', '[email protected]');

    Route::post('/register', array('as' => 'register', 'uses' => '[email protected]'));

    Route::get("/forgot-password", array( "as" => "getPasswordReminder", "uses" => "[email protected]"));
    Route::post("/forgot-password", array( "as" => "postPasswordReminder", "uses" => "[email protected]"));

    Route::get("/reset-password/{token}", array( "as" => "getPasswordReset", "uses" => "[email protected]"));
    Route::post("/reset-password", array( "as" => "postPasswordReset", "uses" => "[email protected]"));
});

// Router for authenticated user
Route::group(array('before' => 'auth'), function()
{
    Route::get('/logout', array('as' => 'logout', 'uses' => '[email protected]'));

    Route::get('/dashboard', array('as' => 'home', 'uses' => '[email protected]'));

    Route::get('/dashboard/addUser', '[email protected]');
    Route::post('/dashboard/addUser', '[email protected]');
});

the guest (group routing for un-authenticated user) and auth (group routing for authenticated user) is predefined in Laravel as filter (app/filters.php).

/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});

Route::filter('auth.basic', function()
{
    return Auth::basic();
});

/*
|--------------------------------------------------------------------------
| Guest Filter
|--------------------------------------------------------------------------
|
| The "guest" filter is the counterpart of the authentication filters as
| it simply checks that the current user is not logged in. A redirect
| response will be issued if they are, which you may freely change.
|
*/

Route::filter('guest', function()
{
    if (Auth::check()) return Redirect::to('/');
});

how to do those Laravel-stuff in Phalcon?

thanks in advance. Edi



8.1k

Laravel | Phalcon


as | name of route

"uses" => "[email protected]" | SiteController is controller postPasswordReset is action

There is very simple :)

Auth from Laravel replaces with ACL from Phalcon.

I'll try it,

thank for your advice