gevman/yii2-router

Elegant Route definitions for Yii2

1.1.6 2018-08-27 19:09 UTC

This package is auto-updated.

Last update: 2024-10-21 03:28:54 UTC


README

Installation (using composer)

composer require gevman/yii2-router
Add this to modules section of Your Yii2 configuration
'modules' => [
    //...
    'router' => [
        'class' => 'Gevman\Router\Module',
        'routeFolder' => '@app/config/routes' //directory where located route files
    ]
    //,...
]
and this to bootstrap section
'bootstrap' => [
    //...
    'router'
    //,...
],

in routeFolder directory You need create Your route files.

ex. web.php with content

$auth = function () {
  if (Yii::$app->user->isGuest) {
      Yii::$app->response->redirect(Url::toRoute(['@login']));
  }
};

Route::prefix('/admin')->routePrefix('admin')->group(function () use ($auth) {
    Route::any('/login', 'default/login')->named('login');

    Route::middleware($auth)->group(function () {
        Route::post('/logout', 'default/logout')->named('logout');
        
        Route::any('/', 'pages/index')->named('admin.pages');
        Route::any('/pages/create', 'pages/create')->named('admin.pages.create');
        Route::any('/pages/<id>/edit/<lang>', 'pages/update')->defaults(['lang' => ''])->named('admin.pages.edit');
        Route::post('/pages/<id>/delete', 'pages/delete')->named('admin.pages.delete');

        Route::prefix('menu')->routePrefix('menu')->group(function () {
            Route::any('/', 'index')->named('admin.menu.index');
            Route::any('create', 'create')->named('admin.menu.create');
            Route::any('<id>/edit/<lang>', 'update')->defaults(['lang' => ''])->named('admin.menu.edit');
            Route::post('<id>/delete', 'delete')->named('admin.menu.delete');
        });

    });
});

after You can use Yii2 native methods for url creation

ex.

echo \yii\helpers\Url::toRoute(['@admin.menu.edit', 'id' => 1, 'lang' => 'hy']);

output will be: /admin/menu/1/edit/hy