建议addons下的controller分层

可按照如下的层级来,比较清晰呢~
addons
|--forms
    |--controller
        |--admin
            |--Index.php
        Index.php
    |--view
        |--admin
            |--index
                |--index.html            |--index.html
配置文件里是:
[
'name' => '新增表单',
'url' => 'forms/admin.Forms/addForm',
'icon' => '',
],
需要修改【application\common\controller\Addon.php】的【fetch】方法
    public function fetch($template = '', $vars = [], $replace = [], $config = [])
{
if ($template == null) {
$template = $this->addonAction;
}

if ($template == 'default') {
$template = APP_PATH . 'common/view/default.' . config('template.view_suffix');

echo parent::fetch($template, $vars, $replace, $config);
} else {
if (strpos($template, "@") === false) {
$tpls = explode('/', $template);
$count = count($tpls);

$suffix = config('template.view_suffix');
$isSuffix = false;

$tpl_suffix = explode('.', $tpls[$count - 1]);
$size = count($tpl_suffix);

if ($size == 2) {
$isSuffix = true;

if ($suffix != $tpl_suffix[1]) {
$suffix = $tpl_suffix[1];
}

$suffixLen = strlen($suffix) + 1;
$template = substr($tpls[$count - 1], 0, -$suffixLen);
}

switch ($count) {
case 1:
$viewFolder = str_replace('.', '/', $this->addonController);
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . $template . '.' . $suffix;
break;
case 2:
$viewFolder = str_replace('.', '/', $tpls[0]);
if (! empty($tpls[0]) && $isSuffix == true) {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . $template . '.' . $suffix;
} else {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . $tpls[1] . '.' . $suffix;
}
break;
}
} else {
$template = substr($template, 1);
$tpls = explode('/', $template);
$count = count($tpls);
$suffix = config('template.view_suffix');
$isSuffix = false;

$tpl_suffix = explode('.', $tpls[$count - 1]);
$size = count($tpl_suffix);
if ($size == 2) {
$isSuffix = true;
if ($suffix != $tpl_suffix[1]) {
$suffix = $tpl_suffix[1];
}
$suffixLen = strlen($suffix) + 1;
$template = substr($tpls[$count - 1], 0, -$suffixLen);
}

$viewFolder = str_replace('.', '/', $tpls[0]);
switch ($count) {
case 2:
if ($isSuffix == true) {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . $this->addonController . '/' . $template . '.' . $suffix;
} else {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . $this->addonController . '/' . $tpls[1] . '.' . $suffix;
}
break;
case 3:
if ($isSuffix == true) {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . strtolower($tpls[1]) . '/' . $template . '.' . $suffix;
} else {
$template = ADDON_PATH . $this->addonName . '/view/' . strtolower($viewFolder) . '/' . strtolower($tpls[1]) . '/' . $tpls[2] . '.' . $suffix;
}
break;
}
}

$config['view_path'] = ADDON_PATH . $this->addonName . '/view/';

echo parent::fetch($template, $vars, $config);
}
}

还需要修改【mp\controller\Call.php】
    public function run(Request $request)
{
if ($this->addon && $this->col && $this->act) {
session('addonName', $this->addon);

$colArr = explode('.', $this->col);
$colLen = count($colArr);
$colArr[$colLen - 1] = ucfirst($colArr[$colLen - 1]);
$controllerFile = ADDON_PATH . $this->addon . '/controller/' . implode('/', $colArr) . '.php';

if (file_exists($commonFile = ADDON_PATH . $this->addon . '/Common.php')) {
include_once $commonFile;
}

Config::set('template.tpl_replace_string.__ADDONSTATIC__', '/addons/' . $this->addon . '/');

if (file_exists($controllerFile)) {
include_once $controllerFile;

$class = '\\addons\\' . $this->addon . '\\controller\\' . implode('\\', $colArr);

if (class_exists($class)) {
$model = new $class;
if (!method_exists($model, $this->act)) {
abort(500, lang($this->act . '方法不存在'));
}

return call_user_func_array([
$model,
$this->act,
], []);
} else {
abort(500, lang($class . '不存在'));
}
} else {
abort(500, lang($this->col . '控制器不存在'));
}
} else {
abort(500, lang($this->addon . '找不到此应用'));
}
}
已邀请:

geeson

赞同来自: luogan129

不错,多谢你的建议,但是你这里还是不够好,不兼容不分层的情况。需要修进的地方还有,addonUrl?希望你能够继续优化。若你优化好了,希望你能联系一下,我会把你的代码集合到新的版本中,新的版本应该在不久后才能发布。新的版本也优化了比较多的东西,还有小程序。

要回复问题请先登录注册