Octobercms,无法从组件重定向到家庭

时间:2019-02-27 20:12:25

标签: redirect components octobercms

如果来自子弹的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

但是它没有重定向。

请咨询

谢谢

2 个答案:

答案 0 :(得分:1)

尝试

在您的组件中:

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

在您的布局中:

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

}

希望对您有所帮助:)

答案 1 :(得分: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');
    }
}