How to Extend a Helper in CodeIgniter? - php

I want to add some new functions to the core string helper, which is found in system/helpers folder. I think there was a 'right' way to do this using MY_String_helper, or something of that sort. I can't remember exactly how it was done though. Any thoughts on this issue?

I found it. Make a file with a name such as this, in the application/helpers directory:
MY_xx_helper.php
E.g:
MY_string_helper.php
Then you can call
$this->load->helper('string');
And it should load all the existing helper functions as well as the new ones you add.

Doing that you can not only add new functions but replace exising helper functions.

For a primary source, in case things change in the future, the CodeIgniter User Guide's Helpers page has a section describing how to extend helpers.

Related

Can you add a prefix to controller file names in Codeigniter?

I like to name my files with a prefix so that when I have them open in an editor I can distinguish what kind of file I'm working with easily.
So instead of naming the controller file home.php or account.php, I want to be able to add a prefix to the file name like controller.home.php.
Is there a configuration option in Codeigniter that let's you do this?
No, there isn't such an option, there's no practical reason for it to exist.
Controllers typically extend the CI_Controller class, so that helps you know what type of class you're editing, if that is of any use to you.
There is no configuration in code-egniter, i guess.
You can not use "." in name, instead of this you can use naming convention like
"controller_home.php" or "controller-home.php"
"controller_account.php"
May be this will help.
in this case, you need rewrite CI router according to your logic

How to use variable from routes.php in controller

In CakePHP have a bunch of unique URL names redirected in routes.php file.
Similar to this:
$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0],
array('controller' => 'Foo','action' => 'bar',3,60));
I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.
How can I reach a variable in routes.php from a controller?
So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.
I'm not cakephp user but simple search shows that there is class called ClassRegistry.
You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.
Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.
You can save key-value pairs with
Configure::write('key','value');
and read it again with
Configure::read('key');

CakePHP: create action hooks for plugins

I'd like to include action hooks similar to those found in Wordpress. I've read the chapter on writing plugins, but I'd to be able to maintain them without altering the code in the parent app.
Is there any baked-in support for this?
If not, is there a good way to do it? I have some ideas but I'm worried I'm going to be reinventing the wheel.
Yes there is.
CakePHP Event System
It works a lot like WordPress's hooks, but only better.
You can register callbacks in different places, but easy way is to do this in the bootstrap.php of the plugin.
When the application loads the plugin it can tell CakePHP to bootstrap it. This is done with this command.
CakePlugin::loadAll(array(array('bootstrap'=>true,'routes'=>true)));
I'm not sure what you're trying to acomplish, but you could do something like this:
Your controller(s) beforeFilter() method is a good place to create certains hooks:
public function beforeFilter() {
parent::beforeFilter(); // don't forget to call parent code
$myHandler->doSomethingInteresting( $this->name, $this->action );
}
where $this->name will give you the name of the controller being called, and $this->action will give you the name of the current action.
I hope it helps a little.

change configure variable dynamically cakephp

Let's say I have this in my bootstrap.php file in a cakePHP application:
Configure::write('Config.language', 'eng');
How can I dynamically change the value of that configuration variable based on user action in a controller of my application? I tried to do the same thing that I did above here in my controller, but that didn't work
Any help?
Try Configure::write('Config.language', 'dut'); for e.g.
This answer from the question suggested by #Ryan Pendleton shows a somewhat correct way to use this directive.
It should be used in the AppController because it gets loaded first - as the parent of all other controllers in the application itself.
I used "somewhat correct" because it is best to validate the language codes ('eng', 'fre', 'dut') in the app/config/routes.php file - go here for more information.
Also do check out this: Internationalization-Localization explanation.

How to use different bootstraping for different modules in zend framework

I have two modules, default and mojo.
After the initial bootstraping code which is the same for both of the modules, I want, for example, to use different layouts for each module (Or use different credentials check etc).
Where do I put this: IF(module=='mojo') do this ELSE do that
If you are using Zend_Application (in ZF1.8) then you should be able to use the module specific configuration options to provide this functionality with a as explained in the relevant section in the documentation.
This would require you to set the layout in the config so it looked something like
mojo.resources.layout.layout = "mojo"
anothermodule.resources.layout.layout = "anotherlayout"
The layout would then be set automatically by the bootstrap.
The other alternative is to use a front controller plug-in that implements the preDispatch() method to set the layout based on the module name.
hmm i havent tried this
http://www.nabble.com/Quick-Guide-How-to-use-different-Layouts-for-each-module-to23443422.html#a24002073
the way i did that now was thru a front controller plugin
something like
switch ($request->getModuleName()) {
case "":
// set layout ...
}
I've looked into the subject a couple of days ago, trying to get it to work on bootstrap config alone. The big problem is that all the bootstrap files are loaded, so it gives some weird results in which layout is used.
My conclusion was that you can have the config in place, but you need to work with FrontController plugins or ActionController helpers. If you want to use config set in the application.ini and you want to load the config trough the bootstrap, helpers is the only way to go. From the helper, you can then load the ActionController and on that execute the getInvokeArgs to load the bootstrap. A lot of hastle... :)
Anyway, I've done a small implementation as an example in a blog post: http://blog.keppens.biz/2009/06/create-modular-application-with-zend.html
Goodluck,
Jeroen

Categories