Octobercms,从组件无法正常工作重定向到主页

问题描述 投票:0回答:2

我正试图从组件重定向,如果slug中的id错误。

从布局运行

function onBeforePageStart(){ $this->Contentloader->getMeta(); }

在组件我有:

public function getMeta(){

//id checking logic goes here

if ($id == null) return Redirect::to('/'); }

检查我看到的dd(Redirect :: to('/'))对象

enter image description here

但它并没有重定向。

请指教

谢谢

redirect components octobercms
2个回答
1
投票

试试这个

在您的组件中:

public function getMeta()
{
   if ($id == null) return false;
}

在你的布局中:

function onBeforePageStart()
{ 
     $meta = $this->Contentloader->getMeta();
     if(!$meta)
          return Redirect::to('/');

}

我希望能帮助你:)


0
投票

组件应该能够处理重定向而无需onBeforePageStart()。这只是一个简单的例子。在这里,我正在检查组件字段是否为空。如果为null则返回'/'。

您可以在组件中执行此操作:确保使用Redirect类use Redirect;

public function defineProperties()
{
    return [
        'useSomething' => [
            'title'             => 'Something',
            'description'       => 'Testing Testing',
            'default'           => '',
            'type'              => 'text',
        ]

    ];
}

public function onRun()
{

    if ($this->property('useSomething') == null) {
        return Redirect::to('/');
    } else {
        $this->page['something'] = $this->property('useSomething');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.