Octobercms,从组件重定向到主页不起作用

Octobercms, redirect to home from component not working

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

运行 来自布局

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

在组件中我有:

public function getMeta(){

//id checking logic goes here

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

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

但它不是重定向。

请指教

谢谢

试试这个

在您的组件中:

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

在您的布局中:

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

}

希望对你有帮助:)

组件应该能够在没有 onBeforePageStart() 的情况下处理重定向。这只是一个简单的例子。在这里,我正在检查组件字段是否为空。如果它是 null 那么 return 到 '/'.

您可以在组件中执行此操作:确保使用重定向 class 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');
    }
}