我正试图从组件重定向,如果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则返回'/'。
您可以在组件中执行此操作:确保使用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');
}
}