我做了很多研究,但没有找到令人满意的答案。
我应该如何使用CodeIgniter库,如坦克豪斯?我已经找到了一些方法,但它们似乎都有些平淡无奇:
第一个选项似乎是最好的,但似乎很难避免复制大量代码(如果我想要一个登录表单,但不想重定向到/auth/ login,会发生什么情况?)
第二种选择也有同样的问题,但更糟。每次我想使用login_form视图时,我都需要包含坦克auth控制器登录函数的逻辑,对吗?
对我来说,最后一个看起来很烦人,似乎是反MVC的,但也许我错了。
发布于 2013-05-09 02:04:17
查看http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
伪代码(参见链接--非常好的例子):
application/config/config.php
$autoload = array('tank_auth');
// add this to the bottom of config.php as per the directions in the link
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}application/core/Private_Controller.php
class Private_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
if(! $this->tank_auth->is_logged_in()){
redirect('auth/login');
}
}
}控制器
class PrivateStuff extends Private_Controller {
function index() {
echo "you are logged in";
}
}-
http://example.com/index.php/privatestuff/
>you are logged in至于视图,您可以使用库附带的视图,自定义视图,或者为您创建自己的视图。
https://stackoverflow.com/questions/16452279
复制相似问题