static function get() { /** * 控制器中可设置当前启用的URL $this->active = array('system','i18n.site.index'); */ if (property_exists(\Yii::$app->controller, 'active')) { $active = \Yii::$app->controller->active; if (!is_array($active)) { $active = array($active); } if ($active) { foreach ($active as $v) { $v = str_replace('.', '/', $v); if (strpos($v, '.') !== false) { $ac[] = url($v); } else { $ac[] = $v; } } } $active = $ac; } $modules = cache_pre('all_modules'); if ($modules) { foreach ($modules as $k => $v) { $file = \Yii::$app->basePath . "/modules/{$k}/Menu.php"; if (file_exists($file)) { $cls = "app\\modules\\" . $k . "\\Menu"; $menus = $cls::add(); foreach ($menus as $key => $val) { if (!$menu[$key]) { unset($actived); if (Arr::array_in_array($key, $active)) { $actived = 'active'; } $menu[$key] = array('label' => __($key), 'url' => '#', 'options' => array('class' => "dropdown {$actived}"), 'template' => "<a href=\"{url}\" data-toggle='dropdown' class='dropdown-toggle'>{label}</a>"); } foreach ($val as $_k => $_u) { unset($actived); if (Arr::array_in_array($_u, $active)) { $actived = 'active'; } $menu[$key]['items'][] = array('label' => __($_k), 'url' => $_u, 'options' => array('class' => $actived)); } } } } } if (!\Yii::$app->user->isGuest) { $menu[] = array('label' => \Yii::$app->user->identity->username, 'url' => '#', 'itemOptions' => array('class' => 'dropdown'), 'template' => "<a href=\"{url}\" data-toggle='dropdown' class='dropdown-toggle'>{label}</a>", 'items' => array(array('label' => __('logout'), 'url' => array('auth/open/logout')))); } // dump($menu);exit; return $menu; }
function init() { parent::init(); language(); hook('controller'); /* * load modules * 加载模块 */ if (!cache_pre('all_modules')) { \app\core\Modules::load(); } }
function init() { parent::init(); language('language_'); if (\Yii::$app->user->isGuest) { flash('error', __('login first')); redirect(url('auth/open/login')); } /* * load modules * 加载模块 */ if (!cache_pre('all_modules')) { \app\core\Modules::load(); } }
static function load() { $all = DB::all('core_modules', array('where' => array('active' => 1), 'orderBy' => 'sort desc,id asc')); $dir = base_path() . 'modules/'; foreach ($all as $v) { $name = $v['name']; $out[$name] = 1; //加载Hook.php $h = $dir . $name . '/Hook.php'; if (file_exists($h)) { $reflection = new \ReflectionClass("\\app\\modules\\{$name}\\Hook"); $methods = $reflection->getMethods(); foreach ($methods as $m) { $action[$m->name][] = $name; } } } cache_pre('all_modules', $out); cache_pre('hooks', $action); }
<?php /** * load modules * 加载模块 */ $modules = cache_pre('all_modules'); //默认系统模块 $modules['core'] = 1; $modules['auth'] = 1; $module['debug'] = array('class' => "yii\\debug\\Module"); if ($modules) { foreach ($modules as $k => $v) { $module[$k] = array('class' => 'app\\modules\\' . $k . '\\Module'); } } $modules = $module; return array('id' => 'hello', 'timeZone' => 'Asia/Shanghai', 'language' => 'zh_cn', 'basePath' => dirname(__DIR__), 'preload' => array('log'), 'modules' => $module, 'components' => array('cache' => array('class' => 'yii\\caching\\FileCache'), 'assetManager' => array('bundles' => require __DIR__ . '/assets.php'), 'log' => array('class' => 'yii\\logging\\Router', 'targets' => array(array('class' => 'yii\\logging\\DebugTarget'))), 'db' => array('class' => 'yii\\db\\Connection', 'dsn' => 'mysql:host=localhost;dbname=books', 'username' => 'test', 'password' => 'test', 'charset' => 'utf8', 'enableSchemaCache' => !YII_DEBUG), 'urlManager' => array('class' => 'yii\\web\\UrlManager', 'enablePrettyUrl' => true, 'suffix' => '.html', 'rules' => array('post/<id:\\d+>/<title:.*?>' => 'post/view', 'posts/<tag:.*?>' => 'post/index', 'admin' => 'core/config/index', 'imagine' => 'image/site/index', '<controller:\\w+>/<action:\\w+>' => '<controller>/<action>')), 'user' => array('class' => 'yii\\web\\User', 'autoRenewCookie' => false, 'identityCookie' => array('name' => 'admin_identity', 'httponly' => true), 'identityClass' => 'app\\modules\\auth\\models\\User'), 'view' => array('class' => 'app\\core\\View', 'theme' => array('class' => 'app\\core\\Theme', 'baseUrl' => '@www/themes/' . $theme), 'renderers' => array('twig' => array('class' => 'yii\\renderers\\TwigViewRenderer', 'cachePath' => '@wwwroot/assets/runtime/Twig/cache')))), 'params' => require __DIR__ . '/params.php');
/** * 加载hook * @ $name 一般为 controller model */ function hook($name) { $hooks = cache_pre('hooks'); if (!$hooks) { return; } $h = $hooks[$name]; if ($h) { foreach ($h as $li) { $cls = "\\app\\modules\\{$li}\\Hook"; $cls::$name(); } } }