【问题标题】:October CMS Routing十月 CMS 路由
【发布时间】:2017-11-08 10:36:04
【问题描述】:

我正在尝试在我的 OctoberCMS 应用中配置路线。我在插件的 Plugin.php 文件中配置路由。 目前我的代码:

public function boot()
    {

        Validator::extend('numeric_for_repeater', function($attribute, $value, $parameters) {
            foreach ($value as $v)
            {
                $validator = Validator::make(
                    $v,
                    [
                        'stock_quantity' => 'sometimes|numeric',
                        'stock_votes_quantity' => 'sometimes|numeric',
                        'value' => 'sometimes|numeric',
                    ],
                    $parameters
                );
                if ($validator->fails())
                    return false;
            }
            return true;
        });

        \Route::get('/oferty/{id}', function ($id = null) {

            $theme =  Theme::getActiveTheme();
            $path = \Config::get('cms.themesPath', '/themes').'/'.$theme->getDirName();
            $this->assetPath = $path;
            $offer = new Offer();
        return \View::make(self::MAMF_PAGE_DIR . 'oferta.htm', ['offer' => $offer->getOfferById($id)]);

        });
    }

但我收到一个错误: View [.var.www.plugins.mamf.mamf2017..........themes.mamf2017.pages.oferta.htm] not found. 因为默认情况下,October 需要插件目录中的视图文件。 我如何在插件目录之外渲染视图,例如themes 路径中的前app/themes/mamf2017/pages/oferta.htm

【问题讨论】:

  • 两点: 1- 您可以在插件的根目录中创建一个名为routes.php 的文件,并将您的路由代码放在那里。 2-您真的需要以这种方式呈现您的页面吗?您可以在themes 目录中创建一个页面,并在页面顶部指定其地址/oferty/{id}
  • 我发现创建文件 routes.php 或编辑 Plugin.php 之间没有区别,因为这两个文件都放在我插件的根目录中。而且我真的不想将所有 - 配置、逻辑和视图 - 放在一个文件中,因为我认为这是一个愚蠢的想法,我想以正常方式将其分开

标签: php routing octobercms


【解决方案1】:

我猜 self::MAMF_PAGE_DIR 是您的应用程序的完整基本路径。比如喜欢

/var/www/vhosts/octdev/themes/responsiv-flat/

总之\View::make需要根目录的绝对路径

现在它将尝试查找配置了 october-cms 扩展名的文件,其 .htm。其他是 .blade.htm.blade 等 ..

所以在你的情况下(视图)文件名是 'oferta.htm' .(dot) 被翻译为 '/' 路径分隔符,所以不要使用它,只使用 'oferta' 所以它会检查 pages 目录中所有可能的值

  • oferta.htm
  • oferta.blade
  • oferta.htm.balde

这个添加 .htm 是自动的,所以你只需要提供视图的名称,它就会自动找到并工作

\Route::get('/oferty/{id}', function ($id = null) {

        $theme =  \Cms\Classes\Theme::getActiveTheme();
        $path = \Config::get('cms.themesPath', '/themes').'/'.$theme->getDirName();
        $this->assetPath = $path;
        $offer = new Offer();
        return \View::make(base_path() . $path . '/pages/' . 'oferta', ['offer' => $offer->getOfferById($id)]);

    });

这是经过测试的,工作正常,希望对您有所帮助。 如果它不起作用,请发表评论。

【讨论】:

  • 我会检查并通知您。谢谢
猜你喜欢
  • 2018-01-26
  • 2016-05-22
  • 2017-09-27
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多