skylineos / yii-menu
Yii2 extention for creating and managing menus
Installs: 77
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: ~8
- yiisoft/yii2: ~2.0.0
- yiisoft/yii2-bootstrap4: ~2.0.10
Requires (Dev)
- codeception/assert-throws: *
- codeception/codeception: *
- codeception/module-asserts: ^1.0.0
- codeception/module-db: *
- codeception/module-phpbrowser: ^1.0.0
- codeception/module-yii2: *
- codeception/specify: *
- codeception/verify: *
- fzaninotto/faker: *
- nathanmac/datetime-codeception-module: ~1.0
- phpunit/php-code-coverage: *
- squizlabs/php_codesniffer: *
- yiisoft/yii2-bootstrap4: ~2.0.6
- yiisoft/yii2-debug: ~2.1.0
README
Installation
Composer:
"skylineos/yii-menu": "~1.0"
Run Migrations:
php yii migrate/up --migrationPath=vendor/skylineos/yii-menu/src/migrations
It is recommended though in no way required you create a migration in your own app to link the properties
createdBy
andmodifiedBy
as foreign keys to the primary keys of your User model (whatever that may be).
Once configured (below), you should be able to access the manager at /menu/menu/index
Configuration
config/web.php
'modules' => [ 'menu' => [ 'class' => 'skylineos\yii\menu\Module', 'viewPath' => '@app/path/to/my/admin/views', // eg. @app/modules/cms/views. The system looks for [menu|menu-item] folders 'roles' => ['@'], // optional yii authorization roles 'dropdownClass' => 'app\path\to\dropDownClass', // @see https://www.yiiframework.com/extension/yiisoft/yii2-bootstrap4/doc/api/2.0/yii-bootstrap4-dropdown /** * Additional templates can be added as such: (namespace => display/friendly name) * @see [Building Templates] */ 'templates' => [ 'skylineos\yii\menu\widgets\SkyMenuWidget' => 'Useful for simple demo', 'app\widgets\menus\OtherMenuWidget' => 'My Other Custom Widget', ], 'targets' => [ // See 'Defining Targets' ] ], ],
Defining Link Targets
Targets (defining what the menu items can link to) can be handled three ways, all within the 'targets' property of the module:
'targets' => [
// Here, slug will be mapped to title, both pulled directly from Content
'app\models\Content' => [
'slug' => 'slug',
'display' => 'title',
'where' => [], // optional
'orderBy' => 'title ASC', // optional
'dropdownGroup' => 'Pages',
],
// This will interpret the model property of Router and map the 'slug' to the 'model.title' based on the fk
// literal => false
'app\models\Router' => [
'slug' => 'slug',
'display' => [
'literal' => false,
'className' => 'model',
'pk' => 'id', // The primary key on the foreign model. If not provided, 'id' will be assumed
'fk' => 'modelId',
'property' => 'title',
'where' => [], // optional
'orderBy' => 'title ASC' // optional
],
'where' => [],
'orderBy' => 'title ASC',
'dropdownGroup' => 'All Routes',
],
// This will load the literal map of ProductCatalog.slug to Product.metaTitle.
// literal => true
'app\models\ProductCatalog' => [
'slug' => 'slug',
'display' => [
'literal' => true,
'className' => 'app\models\Product',
'pk' => 'id', // The primary key on the foreign model. If not provided, 'id' will be assumed
'fk' => 'modelId',
'property' => 'metaTitle',
],
'where' => [],
'orderBy' => 'Product.sortOrder ASC',
'dropdownGroup' => 'Products',
],
],
Usage
This package comes with a helper widget that will process your menu and render it using whatever templates you've configured on your menu items.
Make sure your templates (menuTemplates
and dropDownTemplates
) meet the requirements. See Building Templates
If these requirements do not suit your needs, you are, of course, more than welcome to develop your own widget that renders the items however you like.
<?= \skylineos\yii\menu\widgets\SkyMenuWidget::widget(['menuId' => $menuId]) ?>
Where $menuId
is the id
of the Menu you wish to render.
Building Templates
Menu Templates:
See examples/MenuTemplate.php
in this repository
DropDown Templates:
See examples/DropDownTemplate.php
in this repository