CodeIgniter - accessing $config variable in view - php

Pretty often I need to access $config variables in views.
I know I can pass them from controller to load->view().
But it seems excessive to do it explicitly.
Is there some way or trick to access $config variable from CI views without
disturbing controllers with spare code?

$this->config->item() works fine.
For example, if the config file contains $config['foo'] = 'bar'; then $this->config->item('foo') == 'bar'

Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.

You can do something like that:
$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');

$this->config->item('config_var') did not work for my case.
I could only use the config_item('config_var'); to echo variables in the view

Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.

This is how I did it. In config.php
$config['HTML_TITLE'] = "SO TITLE test";
In applications/view/header.php (assuming html code)
<title><?=$this->config->item("HTML_TITLE");?> </title>

Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];

echo $this->config->config['ur config file']
If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable
$config['50001'] = "your message"
Now I want access in my controller or model .
Try following two cases one should work
case1:
$msg = $this->config->item('ur config file');
echo $msg['50001']; //out put: "your message";
case2:
$msg = $this->config->item('50001');
echo $msg; //out put: "your message"

$config['cricket'] = 'bat'; in config.php file
$this->config->item('cricket') use this in view

If you are trying to accessing config variable into controller than use
$this->config->item('{variable name which you define into config}');
If you are trying to accessing the config variable into outside the controller(helper/hooks) then use
$mms = get_instance();
$mms->config->item('{variable which you define into config}');

Example, if you have:
$config['base_url'] = 'www.example.com'
set in your config.php then
echo base_url();
This works very well almost at every place.
/* Edit */
This might work for the latest versions of codeigniter (4 and above).

Related

Pass variable to default codeigniter controller

I have my codeigniter setup with a default controller. It's accessible as follows:
site.com/index.php
But it's actually:
site.com/project/index
Where index is the default function.
I would like to do the following:
site.com/project7362
But it actually wants:
site.com/project/index/project7362
Where project name is a variable that is pass into the index function. But by default it looks for project-name as a controller. Is there a way to avoid this?
Essentially what I'm hoping to accomplish is to pass a variable directly after the domain name. A user may create a project, and I want that project to be accessible at domain.com/project_id
"Essentially what I'm hoping to accomplish is to pass a variable
directly after the domain name. A user may create a project, and I
want that project to be accessible at domain.com/project_id"
so another way to do this would be to have it like.
domain.com/project/id
this will give you much more flexibility later in your routes for adding different features.
in config/routes:
$route['project/(:any)'] = 'project/view/$1';
in your project controller
function view($id) {
// clean it
$id = htmlspecialchars($id) ;
if ( ! $project = $this->members->returnProjectBy($id) {
$this->showNoResultsFor($id) ; }
else { $this->show($project) ; }
}
OR -- another way to do this would be to put your defined routes first, and then have project be last (because it requires searching on whatever is there)
$route['home'] = 'page/home';
$route['contact'] = 'contact';
// etc etc so you first define your hard coded routes, and then if its not any of those
// you do a search on whatever the value is to find a project
$route['(:any)'] = 'project/view/$1';
so then your link could be
domain.com/id

Set view variables in controller for partials in Zend Framework?

Is there any way to set variables for the partial in the controller level?
Because everytime I need variables inside a partial I always have to pass them:
<?php
echo $this->partial('travels/_steps.phtml',
array('searchHotel' => $this->searchHotel,
'actionName' => $this->actionName))
?>
I would really just like actionName to be available on all partials - for instance.
You could extend the Zend_View_Helper_Partial class to a class that keeps that variable in scope. You would need to override the cloneView() function:
public function cloneView()
{
$view = parent::cloneView();
$view->actionName = $this->view->actionName
return $view;
}
You could use $this->render() instead. With it, you wouldn't need to pass the view variables every time.
Hope that helps,
You could also just sent the current view as a parameter to the partial:
<?php
echo $this->partial('travels/_steps.phtml', array('parentView' => $this));
Then, in the partial:
<?php
$view = $this->parentView;
echo $view->searchHotel, $view->actionName;
In my humble opinion, you're doing exactly what you're supposed to do - passing just those variables you'll need in the partial.
If this causes you pain, perhaps you might consider that you're using partials unnecessarily.
Or, put another way, if you want to have some variable available in all partials then perhaps the partial is not where you should be using those variables.
Maybe have a look at Placeholders and rethink how you go about rendering your views.

Constants in Kohana

I've used codeigniter in the past but on my current project I'm making the switch to Kohana. What is the best practice on constants?
In codeigniter there is the actual constants.php, but going through Kohana's source I'm not seeing something similar.
Never used kohana, but after a quick googling I find that you can use the config API to create your own config that will house the constants you need.
This thread suggests that if you are storing database sensitive items, to place them in the database.php config, etc.. making them relative to the type of data they are storing.
I'm familiar with Kohana, but not CI so much, so I'm guessing a bit to what you mean by 'constants.' I believe the closest thing to this is indeed Kohana's config API. So, if you wanted to make templates aware of some site-wide constant like your site name, that's a great thing to use the config API for.
To accomplish this, you'll need to create a config file under your /config folder, probably in the /application directory. What you call it isn't very important, but since it contains site information, let's call it site.php.
To quickly get going, here is what you'll want to have in that file:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
// Your site name!
'name' => 'Oh me, Oh my',
);
Now, you can bring this in to a template by doing something like:
A better way to do this (using dumb templating) would be to assign this as a template variable in your Controller. So, assuming you have some default controller set up, the code would be:
public function action_index() {
$this->template->site_name = Kohana::config('site.name');
}
And then your template would have something like this:
<title><?php echo $site_name; ?></title>
Kohana's config API is interesting because it is hierarchical, meaning you can override and merge new configuration values on top of existing config structures. When you call Kohana::config('site.name'), the engine looks through all the config files named site.php, runs all of those config files and merges the results in to an array. The application-level config files will overwrite modules, which will overwrite system, etc... Then, based on that result array, Kohana will attempt to find the 'name' key and return it.
Assuming you want global constants...the following worked well for me.
application/config/constants.php:
define('MY_COOL_CONSTANT', 'foo');
return array();
index.php:
Kohana::$config->load('constants');
MY_COOL_CONSTANT should then be available globally.

Where to store application constants in Zend?

I have a few constants that I'll need to define for my application, for example, SITE_KEY which would contain a random key for salt passwords.
I'm not sure where I should define those, though. I thought of placing them in public/index.php but that seems a bit messy. Is there a specific place where they should go, according to Zend or something?
Thanks
EDIT
I'm trying to do it this way:
In my application.ini I have this:
siteglobal.sitekey = "test"
and in my bootstrap.php file:
protected function _initGlobals()
{
$config = $this->getOptions();
define('SITE_KEY', $config['siteglobal']['sitekey']);
}
Still, this isn't working, when I try to echo SITE_KEY in my controller like this:
echo SITE_KEY;
It doesn't show anything. Any ideas?
In my application, I'm combining both Haim's and Yeroon's approaches. I'm storing application constants in my application.ini, then parsing them in the Bootstrap and putting them into Zend_Registry.
So, in the application.ini
constants.site_key = my_site_key
In the Bootstrap
protected function _initConstants()
{
$registry = Zend_Registry::getInstance();
$registry->constants = new Zend_Config( $this->getApplication()->getOption('constants') );
}
Then I can access these constants from any place in the application (model, controller, etc.)
Zend_Registry::getInstance()->constants->site_key
Have you tried Zend_Registry? It's used to store application-wide values, objects etc.
Store:
Zend_Registry::set('index', $value);
Retrieve:
$value = Zend_Registry::get('index');
in
application/configs/application.ini
myvar = myvalue
after get them like :
$myvar = $application->getOption('myvar');
read more ....

Load a controller into an other controller in cakephp

im developing a web application, using multiple pages, each with their own controller.
The problem now is that there are some variables in a controller, created for one page, that are required for an other page ( with a different controller).
Therefor i need to load one controller into the other one.
I did this by adding
App::import('Controller', 'sections');
$sections= new sectionsController;
$sections->constructClasses();
to the controller, but this doens't seem to work..
Maybe u guys have some ideas?
Thnx in advance!
I think there're some misunderstandings in your mind about MVC architectural pattern.If you need some bullet for your gun,just get the bullet itself,and it's not neccessary to get another gun with it.So I hope you understand loading controller is really a bad idea.
Also if you want some variables accessable by all controllers,as Gaurav Sharma
mentioned ,you can use Configure::write() to store data in the application’s configuration app/config/core.php.e.g
Configure::write('somekey',$someval);
Then you can get $someval by Configure::read('somekey') in any controller.
You can use any one of the methods below to access a variable anywhere in the cakePHP application.
1.) use the configuration class OR
2.) use session for those variables
I've been working on this this morning. I actually get the controller name from a database, but I've changed it to use variables instead.
$controller_name = "Posts"; // the name of the controller.
$action = "index"; // The action we want to call.
App::import('Controller', $controller_name);
// Now we need the actual class name of the controller.
$controller_classname = $controller_name . 'Controller';
$Controller = new $controller_name;
$Controller->variable_name = "something"; // we can set class variables here too.
// Now invoke the dispatcher so that it will load and initialize everything (like components)
$d = new Dispatcher();
$d->_invoke($Controller, array('pass'=> '', 'action' => $action));
// And exit so it doesn't keep going.
exit(0);
I honestly didn't bother figuring out what 'pass' is for (I assume variables), but it throws a warning without it.
You will also need to explicitly call $this->render in your $action.

Categories