Welcome Guest, Not a member yet? Register   Sign In
Global Strings!
#1

[eluser]jordanarseno[/eluser]
Hello everyone,

I created a php file called site_specific.php in CI/applications/config/ to store site specific information that will never change:

Code:
<?php
$site = array(
    "title" => "Title",
    "subtitle" =>"Subtitle",
    "copyright" =>"Copyright 2010 - JoAr"
);

?>

Now at the top of my header.php view I want to be able to include this file.
So used a simple php include...

Code:
<?php include(base_url() . "application/config/site_specific.php");?>

Now I'm getting errors:

Message: include() [function.include]: http:// wrapper is disabled in the server configuration by allow_url_include=0

Message: include(http://localhost/CI/application/site_specific.php) [function.include]: failed to open stream: no suitable wrapper could be found

If I use the following...it works, but it is undesirable!:
Code:
<?php include("/opt/lampp/htdocs/CI/application/site_specific.php");?>

I tried moving site_specific.php out of the config folder,because maybe CodeIgniter locks down the config directory? - But the errors still flow.

I know this goes against the MVC pattern, but it seemed like the easiest way to get static information to the view without making a mess of all my controllers.
#2

[eluser]kaejiavo[/eluser]
Hi,

you can use a config file e.g. CI/applications/config/myconfig.php with the following syntax:

Code:
$config['title'] = 'Title';
$config['subtitle'] = 'Subtitle';
...

To use it in your controllers:
Code:
$this->load->config('myconfig', TRUE);
$title = $this->config->item('title', 'myconfig');
...

Marco
#3

[eluser]kaejiavo[/eluser]
btw. if you use the same information in all your controllers, you maybe want to use a MY_Controller as described by Phil Sturgeon here

Marco
#4

[eluser]jordanarseno[/eluser]
Thank you! - That was a very informative article.
I left a comment on Phil's blog, but his processing engine destroyed half of my message, and it won't let me post a new one Sad

Therefore, question for you, and you...and you!

I followed Phil's method - I now have a MY_Controller class.

Now, he mentioned at the bottom of his post, I quote:
Quote:You can also use $this->load->vars('foo', $bar) in your Base Controllers to set values that are only available in your views.

So in MY_Controller, I have the following:

Code:
$arr = array (
'test1' => '123',
'test2'  => 'abc'
);

$this->load->vars($arr);

And in my view:

Code:
<?php  echo $arr['test1'];?>

I'm getting the error: Message: Undefined variable: arr

Why!!
#5

[eluser]Twisted1919[/eluser]
Code:
echo $test1 ;
because the $arr is extract()-ed
#6

[eluser]jordanarseno[/eluser]
Hi Twisted,

Thanks - but: Message: Undefined variable: test1 now...
#7

[eluser]Twisted1919[/eluser]
Weird, but what if, you do like so :
Code:
//MY CONTROLLER
class MY_Controller extends Controller{

    public $data = array();
    [...]
    public function __construct(){
       parent::Controller();
       $this->data['test1'] = 'A string';
    }

}
//Controller file
class Something extends MY_Controller{

      public function say_something()
      {
           $this->load->view('a view',$this->data);
      }
}
In this way, echoing $test1 in your view, works ?
#8

[eluser]jordanarseno[/eluser]
Hey Twisted,

I played around with your code a bit - eventually tweaked my scripts so that it works perfectly! Smile
Your help is much appreciated - this global string stuff is going to really make my life easier; no longer do I have to send additional data from controllers to views!

Keep it DRY!
#9

[eluser]John_Betong[/eluser]
Maybe this will get you over the hump and be able to proceed to the next "Gotcha"...

To use site specific information that will not change, you can store the information in your MY_Controller file:

Code:
// My_Controller
Class MY_Controller extends Controller
{
public $site = array
(
    "title" => "Title",
    "subtitle" =>"Subtitle",
    "copyright" =>"Copyright 2010 - JoAr"
);

// your view
echo $this->site['title'];
echo $this->site['subtitle'];
echo $this->site['copyright'];
 
 
 
#10

[eluser]Twisted1919[/eluser]
Glad it helped, actually this is the way i do it(and think the most of programmers), i have a $data var in MY_Controller, then when passing data for the view i just $this->data['something'] = 'something'; then pass $this->data when loading the view.




Theme © iAndrew 2016 - Forum software by © MyBB