简体   繁体   English

Codeigniter重定向/加载视图

[英]Codeigniter Redirect/Load view

I really want to understand whats the best use of both functions. 我真的很想了解这两种功能的最佳用法。

Controller: (After much processing, lets skip directly to creating the session) 控制器:(经过大量处理后,让我们直接跳至创建会话)

$isLoggedin = array( 'user_name' => $row['username'] , 'user_type' => 'user' ,  'is_loggedin' => TRUE);
$this->session->set_user($isLoggedin); 
redirect('controller/index');

in the controller, 在控制器中

public function __construct()
{
parent::__construct();
    //should i check for the session here?
}

function index(){
 // or should i check here?
}

In the login controller, after creating the session, i passed it to the main controller, Before accessing the homepage, i want to verify if the session['is_loggedin'] == TRUE that signifies the session that was created = logged in. 在登录控制器中,创建会话后,我将其传递给主控制器。在访问主页之前,我要验证session['is_loggedin'] == TRUE是否表示已创建的会话=已登录。

My confusion is, since the constructor function is loaded first right everytime you call the controller. 我的困惑是,由于每次调用控制器时都会首先加载构造函数。 Please correct me if im wrong. 如果我错了,请纠正我。 So in the redirect in the log in controller, i shouldnt call the index? 因此,在登录控制器中的重定向中,我不应该调用索引吗?

should i just call the controller there then let the constructor handle, then in the constructor, like this. 我应该在那儿调用控制器,然后让构造函数处理,然后在构造函数中这样处理。

public function __construct(){
parent::__construct();
if($session['is_loggedin'] == TRUE)
$this->load->view('homepage');
else
redirect('Login');
}

So i guess i wont need the index anymore right? 所以我想我将不再需要该索引了吗? Is it okay to call the controller that way? 这样可以调用控制器吗? in the redirect, also do i have to pass the $session from controller to controller? 在重定向中,我也必须将$ session从控制器传递到控制器吗?

Use constructor to load common libraries and helpers which are needed for the other functions in that particular class. 使用构造函数加载该特定类中其他函数所需的公共库和帮助程序。 Index function works as a default function when there is no other function is mentioned. 当未提及其他功能时,索引功能将作为默认功能。 For example you can do like this 例如,您可以这样做

public function index(){
  $this->login();
}

public function login(){
  login code

}

so this way your base url/class name/ and base url/class name/ login both will work. 因此, your base url/class name/base url/class name/ login都可以使用。 If you autoload session library in your application/config/autoload.php file you don't need to worry about session, just you need to set an encryption key in config.php file 如果您在application/config/autoload.php文件中自动加载会话库,则无需担心会话,只需要在config.php文件中设置加密密钥即可。

If you don't want to autoload session then you need to load session manually on every class's constructor where you need to maintain session. 如果您不想自动加载会话,则需要在需要维护会话的每个类的构造函数上手动加载会话。

You better check for the user if he has a logged in session in your constructor function before loading any libraries or helpers. 在加载任何库或帮助程序之前,最好检查用户在构造函数中是否具有已登录的会话。 but make sure session and database are loaded to check users login status. 但请确保已加载会话和数据库以检查用户的登录状态。 this can help you to prevent any other unauthorized access to the other functions and resources of your website, cause one might just type the name of some other functions into the URL directly! 这可以帮助您防止对网站的其他功能和资源进行任何其他未经授权的访问,因为您可能只是直接在URL中键入其他功能的名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM