Welcome Guest, Not a member yet? Register   Sign In
Execute script automatically on every page?
#1

[eluser]Sillysoft[/eluser]
Hello,
Im just getting into CI so please excuse the noobie in me. Anyway Im planning to rewrite a site using CI and was wondering how would I execute a script on every page automatically? Currently the site queries the database to the current site id based on information passed on it and the id returned is used to pull up information for every page specific to that site id. I have a global include script that gets included into all the pages so I dont have to write the code on every page. In CI do I just create a model and add to the AutoLoad config file and access the model in every controller?

Thanks
Silly
#2

[eluser]gyo[/eluser]
Yeah, autoload is the key Wink
#3

[eluser]Sillysoft[/eluser]
But that just autoloads the actually model, I still would have to call the model on every page correct?
#4

[eluser]gyo[/eluser]
You can always execute a method from the model constructor like: $this->init();
So on each page load the init() will be executed.
#5

[eluser]jedd[/eluser]
Hi Sillysoft,

You might want to read this [url="http://ellislab.com/forums/viewthread/109698/#553141"]most excellent and thoughtful response to a similar question, written by a really nice guy[/url] on one way to address this problem.

I note that you're unsure if you want to do this in the controller or the model - you may want to describe exactly what you want to achieve with this code, as there may be better ways of doing this.
#6

[eluser]Sillysoft[/eluser]
[quote author="jedd" date="1238728055"]Hi Sillysoft,

You might want to read this [url="http://ellislab.com/forums/viewthread/109698/#553141"]most excellent and thoughtful response to a similar question, written by a really nice guy[/url] on one way to address this problem.

I note that you're unsure if you want to do this in the controller or the model - you may want to describe exactly what you want to achieve with this code, as there may be better ways of doing this.[/quote]

Hello,
First thank you all for your responses. With regards to describe exactly what Im looking to do. The site/code is already there, but written in custom code. Im wanting to rewrite it using CI.

Basically the site itself displays information about cities, each city is considered a different website, but each domain points to the same directory of code. How the site tells the difference on what template to show and what data to show is determined by the domain name. So currently the domain name is grabbed from the url when visiting the site then with that domain the site table in the database is queried for a match.

If a match is found then I grab the unique ID for that particular site that is found, that id is then returned to the script as a variable and used throughout the site to determine what information gets loaded from the template itself to the data it shows. So data from city1 only shows for city1 and so on and so on. This "action" needs to happen on every page.

Is that the information your looking for?
#7

[eluser]jedd[/eluser]
Ah, good, okay. It's not the kind of thing that you want to extend controllers for, then.

It'll be a routing / htaccess hybrid I presume. That side of things confuses me, particularly if you're going to have to re-write your base_url to match the domain the user came in on (or was redirected to). But now you've described it thus, it'll be easier for one of the apache/CI-routing gurus to give you some pointers.
#8

[eluser]Sillysoft[/eluser]
[quote author="jedd" date="1238729306"]Ah, good, okay. It's not the kind of thing that you want to extend controllers for, then.

It'll be a routing / htaccess hybrid I presume. That side of things confuses me, particularly if you're going to have to re-write your base_url to match the domain the user came in on (or was redirected to). But now you've described it thus, it'll be easier for one of the apache/CI-routing gurus to give you some pointers.[/quote]

Thanks for the information. While I wait for someone to respond with some help on that part I will search the forums. Sounds complicated. To bad you couldnt just dynamically set the base url.
#9

[eluser]Sillysoft[/eluser]
[quote author="jedd" date="1238729306"]Ah, good, okay. It's not the kind of thing that you want to extend controllers for, then.

It'll be a routing / htaccess hybrid I presume. That side of things confuses me, particularly if you're going to have to re-write your base_url to match the domain the user came in on (or was redirected to). But now you've described it thus, it'll be easier for one of the apache/CI-routing gurus to give you some pointers.[/quote]

I found this post here:
http://ellislab.com/forums/viewthread/106992/

Using this everything seems to work in loading the template by domain name. So when I went to one domain it worked with the correct domain and it worked going to a different domain.
#10

[eluser]gyo[/eluser]
Sorry Sillysoft but your first question was misleading for me.
Anyway, as jedd said, check the Routing feature which allows for this kind of fine-tuning.

cheers!




Theme © iAndrew 2016 - Forum software by © MyBB