Welcome Guest, Not a member yet? Register   Sign In
How to stop CI from continuing?
#1

[eluser]louis w[/eluser]
Is there a way in my constructor to tell CI to stop further execution and not call the requested method?

Using end() causes CI not to output to the page because the output library would not get called.
#2

[eluser]sophistry[/eluser]
look at _remap()
#3

[eluser]louis w[/eluser]
Thank you for the suggestion, won't work in this instance thou. I was hoping not to effect the normal execution if possible. Let me go into more details.

I would like to have a conditional in my constructor which checks to see if the user is logged in. If not it would present a log in form and stop the page from displaying as normal (but not change the url). But if the user is logged in, not do anything.
#4

[eluser]sophistry[/eluser]
oh. i see. it sounds like your specific requirement is "check if a user is logged in" - there are many, many ways to set this up.

your original post: "stop further execution and not call the requested method" is the exact definition of _remap().

anyway, you could look at hooks, but some argue they lead to 'invisible' code.

also, you could search the forums for your pertinent strings: login, authentication, admin, etc... there are several libraries that can help you do this.

good luck.
#5

[eluser]louis w[/eluser]
Thanks I will look into hooks. That's a good idea.

For the time being i am using redirect and kicking them to a separate url. It works, for the time being but I would prefer to keep the url consistent.
#6

[eluser]ray73864[/eluser]
why not something like this in say your 'index' function of the controller:

if not logged in
if not login form submitted
display the login view
else
do the login stuff and then redirect back to this function
else
display the normal view

no redirect will happen, and you can have the login view's form submit to the controllers index function
#7

[eluser]louis w[/eluser]
This is a large application with many levels of extended controllers. I do not want to put a conditional on every single method.
#8

[eluser]ray73864[/eluser]
[quote author="louis w" date="1227163872"]This is a large application with many levels of extended controllers. I do not want to put a conditional on every single method.[/quote]

ahhh, k, you didn't mention that before so i just put up a general way of solving the problem.

course, you could put it into the base constructor.
#9

[eluser]sophistry[/eluser]
sorry to harp on _remap() again, but it does have some pretty nice uses. i think you might like this example of using _remap() in an extended controller

this controller's purpose is to help you migrate an existing site, but it shows a key technique: using _remap() to transparently map to methods at any level of class heirarchy (without changing the URL). i tend to agree with the people who criticize hooks because the code becomes invisible; using _remap() in your parent controller lets you do any kind of routing in there.

in my case, i needed to easily drop-in an existing (largely static, but some inline PHP) website right into a CI install and have it work properly without major mods to the code base.
#10

[eluser]mihailt[/eluser]
come on guys...

all you need is to extend a controller.
create something like:

Code:
class APP_Controller extends controller{
    public function __construct(){
        parent::__construct();
        

        if(!$this->session->userdata('somedata')){
        // do what you what here - implement logic, load views etc.      
        }
    }
}
place it in library folder, and then make all controllers to extend APP_Controller instead of Controller.

also if you would check user guide on views, you would see that $this->load->view() method accepts 3 parametrs and third allows to store view in a variable.

good luck.




Theme © iAndrew 2016 - Forum software by © MyBB