Web Kohana-每个控制器上有一个函数
我在科哈纳3.3有一个项目。 我有很多控制器、模型等 现在,我想添加一个功能-关闭所有用户的整个网站 我可以在哪里添加函数,例如,将用户重定向到 例如:Web Kohana-每个控制器上有一个函数,web,kohana,Web,Kohana,我在科哈纳3.3有一个项目。 我有很多控制器、模型等 现在,我想添加一个功能-关闭所有用户的整个网站 我可以在哪里添加函数,例如,将用户重定向到 例如: function check(){ $isClose = DB::query(.....) if($isClose) header("Location: http://mypage.com/website_is_close"); return false; } 谢谢:)在Controller\u Base中,所有其他控
function check(){
$isClose = DB::query(.....)
if($isClose) header("Location: http://mypage.com/website_is_close");
return false;
}
谢谢:)在
Controller\u Base
中,所有其他控制器都从扩展而来。例如
文件application/classes/Controller/Base.php
class Controller_Base extends Controller_Template {
public function before()
{
$isClose = DB::query(.....)
if($isClose)
{
HTTP::redirect("http://mypage.com/website_is_close");
exit ;
}
parent::before();
}
}
所有其他类别均应从该类别扩展,例如
class Controller_Home extends Controller_Base {}
我个人也在每个子目录中使用它,例如
// As all controllers in the user folder probably need to be supplied with a user anyway
class Controller_User_Base extends Controller_Base {}
class Controller_User_Profile extends Controller_User_Base {}
我认为更好的方法是在路线列表的开头添加一条“综合”路线 它将捕获所有URL并指向您将创建的控制器。这比黑客攻击基地控制器要干净得多 这个看起来怎么样
Route::set('closed', '(<url>)', array('url' => '.*'))
->defaults(array(
'controller' => 'Closed',
'action' => 'index',
));
Route::set('closed','()',array('url'=>'.*))
->默认值(数组)(
“控制器”=>“已关闭”,
“操作”=>“索引”,
));
Hmm。。我在before()函数的/system/classes/Kohana/Controller.php中添加了我的函数。它的工作:)但这是正确的吗?不。你不应该改变系统代码。别在这里走捷径!重构!我使用Controller\u模板
类来实现这些目的。所有其他控制器都扩展了它,并拥有所有非私有方法。要正确添加新的父方法,请参考Kohana级联文件系统docs()。@s.webbandit,只要该控制器位于application/classes/Controller/Template.php
中,它就是有效的:),这可能是OP最简单的解决方案。谢谢!