Forcing CodeIgniter to send view and stop working

房东的猫 提交于 2021-02-09 08:20:28

问题


Hello I'm using inherited controllers. These are my controllers:

-baseAdminController:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class _BaseAdminController extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');

        $calledFunction= $this->router->fetch_method();
        if ($calledFunction!= 'loginView' && $calledFunction!= 'doLogin') {
            $this->checkSession();
        }
    }

    public function checkSession() {

        if ($this->session->userdata('loggedIn') == false) {
            $this->load->view('admin/loginView');
        }
    }
}

And my derived Admin Controllers:

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class AdminController extends _BaseAdminController {

    public function indexView() {
        $this->load->view('admin/indexView');
    }
}

When i'm tring to login, CodeIgniter shows me both admin/loginView and admin/indexView. Because i'm checking session status at constructing my derived controller. How can i prevent to loading second view?

Thank in advance..


回答1:


To answer your question, you could have done the following into your checkSession method:

public function checkSession() {

    if ($this->session->userdata('loggedIn') == false) {
        echo $this->load->view('admin/loginView', array(), TRUE);
        exit;
    }
}

Explanation: If you pass the third argument as TRUE, it will return the content of that file. Read ellislab.com for more info.




回答2:


I hope this is related to your question:

I recently wanted to have a bunch of controllers extend a parent controller class (MY_Acl_Controller) that checked that the current logged-in user deserved access to each method (using a homegrown ACL library). The check was to be initiated in MY_Acl_Controller's constructor, so it would run on every request.

I wanted to set the Output class's output to the result of loading a view, then display the view and exit, but because this process was NOT executed in a routed controller method, I couldn't just call $this->load->view('errors/access_denied') and then return from the constructor function... CI would then carry on executing controller code.

So I created a MY_Output class, extending CI_Output, and added to it a public function display_with_exit():

public function display_with_exit()
{
    $this->_display($this->final_output);
    exit;
}

Then, in MY_Acl_Controller's constructor:

...
if(!$user_deserves_access)
{
    $this->load->view('errors/access_denied');
    $this->output->display_with_exit();
}

Maybe that might be useful to someone?

The advantage of this approach is that the Output class's _display() function sends all HTTP headers you'd like it to, as per any normal response.




回答3:


Avoid exit; / function exit; or die; will terminate execution, its better practice in only debugging your code.

Try like below.

public function index(){
  if($xx) return TRUE;
}


来源:https://stackoverflow.com/questions/17225251/forcing-codeigniter-to-send-view-and-stop-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!