Method/function overrides in Codeigniter - php

I want to override the validation_errors() method of the form helper in CodeIgniter for one controller only, so that it will display a single error message (as a sentence in plain english) instead of the detailed line item summary. I've tried defining a validation_errors() function in my controller, which is what I usually do with Silverstripe's Sapphire framework, but this won't with CI.
What's the best way to override methods for a case by case basis?

You can create a MY_Form_helper.php in your application/helpers/ folder and create that function in there and then when you load the form helper, it should also load your function.
For more information read the '"Extending" Helpers' section on: http://codeigniter.com/user_guide/general/helpers.html

Related

How to call viewHelper via OPT

I work with Zend Framework 1.12 and Open Power Template.
In ZF I made view helper InfolineData with method getDispayInfoline($lang) (class name Callcenter_View_Helper_InfolineData) and I pass this helper via controller to view.
Controller Code:
$this->view->openHours = $this->view->getHelper('infolineData')->getDispayInfoline($lang);
View Code:
{$openHours}
How can I make similar call using only Open Power Template?
Ok, I find solution
$this->view->getHelper('infolineData')->getDispayInfoline($lang)
is equal to
{u:$view::getHelper('infolineData')::getDispayInfoline($view::language)}

Removing controller name and function namein URL in Codeigniter CMS

I am building a cms in codeigniter and i want to remove controller name and function name form the url and just print the alias.
I will be having two controllers one for static pages and other for blog posts with categories.
Please help, Suggestions reagrding modification of two controllers are also welcome.
You will need to override the default 404 controller in application/config/routes.php.
$route['404_override'] = 'content';
Any request that can't be mapped to a controller will be passed to the application/controllers/content.php controller
Your Content controller, or whatever you decide to call it, will parse the uri [$this->uri->segment(1)] and check for a matching reference in your CMS database.
If there is no match in the database, then you can look for a static view in the views folder and load it.
if(is_file(FCPATH.'views/'.$this->uri->segment(1).'.php')) {
$this->load->view($controller,$this->data);
}
If no static view is found, and there is no matching content in the db, call the show_404() function.
Using this method, you will keep the default CI functionality of uri mapping, so at any time, you can add controllers as you normally would and the app will perform like a vanilla CI install.

ACL check in the view

Well in ZF1 there was the isAllowed view helper to check the ACL in the view.
But I can't find an equivalent of it for ZF2. The closest I got was the ZF2 navigation view helper, but the problem with that is that it requires a AbstractPage. I just want to throw a resource/privilege to it, example:
$this->allowed('resource', 'privilege');
Is there not such a view helper, or am I looking at this all the wrong way?
Well I couldn't find any view helper, neither got any response so I wrote my own view helper.
isAllowed view helper code

Modification Yii CRUD generate (Gii) form template

I want to change the Gii template follow my own template that I have where I found the code to change
<div class="errorMessage">....</div> become my own template style??
I have changed most of the gii templates style follow mine but i have not found the line to change the "div" error message on : framework\gii\generators\crud\templates\default
The main view file for CRUD generation is in framework\gii\generators\crud\templates\views\index.php. The form is generated using CCodeForm, and the error messages are generated using the $form->error() method.
You can customise these considerably by just passsing parameters to the $form->error() method as described here, or you can override the $form->error() method by creating your own class which extends CCodeForm, but that may have unintended results!
I'd suggest, for ease, that you pass parameters to each of the $form->error() methods that are called in the view file.
To do this, follow these steps;
Create a folder 'gii' in your protected folder
Create a folder within that called 'crud'
Into that folder copy the entire contents of `framework/gii/generators/crud. These files will now override the default files from gii.
Open protected/gii/crud/views/index.php
Find all the error fields. They look like <?php echo $form->error($model,'controller'); ?>
Add an array of html options to the error declaration, so it looks like <?php echo $form->error($model,'controller', array('class' => 'alert alert-error')); ?>
Thats it! This method has the benefit that you haven't modified the core framework files, so if you update yii your changes will not be overwritten. For more information, have a look at this http://www.yiiframework.com/doc/guide/1.1/en/topics.gii

Using actionHelper without returning layout in output

I call an action helper in one of my views using the following code
echo $this->action('foo', 'bar');
The fooAction in the barController does its thing and outputs a list of pages. However, the list has the layout in the output again, which is mightily irritating. If I disable the layout in the fooAction, this causes layout to be completely disabled on the live side, as well.
I'm vexed. I could just create a view helper, and there are many ways around this, but out of curiousity I was wondering if anyone had a solution to this.
From the ZF Reference Guide on Action ViewHelper
The API for the Action view helper follows that of most MVC components that invoke controller actions: action($action, $controller, $module = null, array $params = array()). $action and $controller are required; if no module is specified, the default module is assumed.
Modify your controller to accept a param that controls whether the action should disable the layout. When using the action helper, pass this control flag.
On a sidenote: using the Action ViewHelper is considered bad practise as it will go through the entire dispatch process again and this will slow down your app. If possible, try to access the model directly.

Categories