📜 ⬆️ ⬇️

A couple of goodies for CodeIgniter

In 2012, I didn’t even guess about the word combination of OOP, but I clearly understood that writing websites and in the near future web applications would require already prepared solutions, as the number of tasks increased, and there was not enough time to implement them. My choice fell on CodeIgniter because his documentation took pity on me, throwing away the words that were difficult for a weaker beginner. Frankly, I still have not met the same convenient and understandable documentation. Since then, relatively little time has passed. But I decided to share not numerous accumulated knowledge with users of Habr.

At the moment I am using CodeIgniter v2.2.0.

image

Routing:
')
People who use this framework know exactly how routing in CI works. However, I often wondered how all the same to redirect all requests to a single controller.

Everything just needs to add the following line to the end of the /aplication/config/routes.php file:

$route['(:any)'] = $route['default_controller']."/$1"; 

After which we get:

 $route['default_controller'] = "main"; $route['404_override'] = ''; $route['(:any)'] = $route['default_controller']."/$1"; 

In this case, all requests, be it http // mysite / user or http // mysite.loc / page , will be completely redirected to http // my_site / main / user and http // mysite.loc / main / page, respectively.

Fine! But what if we need to leave redirects to other controllers, for example: user, page, admin . In this case, I use the following scheme:

 <?php //    . $controllers = array( "user", "admin", "page" ); foreach($controllers as $controller){ $route[$controller] = $controller; $route[$controller.'/(:any)'] = $controller.'/$1'; } $route['default_controller'] = "main"; $route['404_override'] = ''; $route['(:any)'] = $route['default_controller']."/$1"; 

Now we have a main controller and additional ones, to which we can just as easily contact. But what if I have only one controller responsible for the output of CSS, JavaScript and images .

 <?php //    $systems_redirect = array( 'css', 'js', 'image' ); $route['('.implode('|',$systems_redirect).')'] = 'systems/$1'; $route['('.implode('|',$systems_redirect).')/(:any)'] = 'systems/$1/$2'; //    . $controllers = array( "user", "admin", "page" ); foreach($controllers as $controller){ $route[$controller] = $controller; $route[$controller.'/(:any)'] = $controller.'/$1'; } $route['default_controller'] = "main"; $route['404_override'] = ''; $route['(:any)'] = $route['default_controller']."/$1"; 

This is my final routes.php file.

Error 404 output:

Some time ago I wondered. How to redirect 404 error to the controller in order to display some data there, because as we know, CodeIgniter displays only a small pre-prepared html page without the ability to load some information from the database into it. Or work with libraries.

It would seem that the solution is painfully simple. In the previously mentioned routes.php file, change the value of the $ route variable ['404_override'] = ''; on the name of a previously prepared controller, for example my404.php. But using this implementation, I suddenly came across one very unpleasant thing.

The fact is that if you need to call the show_404 () function from an external controller, you will receive an old pre-prepared html page /aplication/errors/error_404.php which is unlikely to please you.

The best solution to this problem, I decided to extend the standard class CI_Exceptions using the automatically loadable file /aplication/core/MY_Exceptions.php will replace the standard function show_404 () :

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Exceptions extends CI_Exceptions { public function show_404() { $CI =& get_instance(); $CI->output->set_status_header('404'); $CI->output->set_output("error_404"); } } 

In this case, extending the standard class CI_Exceptions, we get access to the function get_instance (); which in turn gives us the opportunity to fully use all the classes of the helper model, etc.

Getting errors from the CI_Form_validation library:

Imagine that we submit a form using ajax and wait for a return in json format. At the same time checking the data using CI_Form_validation. And the library seems to be working fine, but returning errors to an array for converting to json is not possible since the only function for returning errors $ this-> form_validation-> error_string () returns a string with all the errors from which later you will not get errors for each transmitted value. Of course, there is another function $ this-> form_validation-> error (), but to use it you will need a completely unnecessary cycle. And here is the solution. Create the /aplication/libraries/MY_Form_validation.php file with which I will extend the functionality of the library, as well as I can add new functions to check the input data.

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { function __construct($config = array()) { parent::__construct($config); } function error_array() { if (count($this->_error_array) === 0) return FALSE; else return $this->_error_array; } } 

Thus, we get the function $ this-> form_validation-> error_array () which will return us an array of errors. For example:

 $errors = array( "username" => "   ", "email" => "   " ); 

Thanks for attention! Hope was useful.

Source: https://habr.com/ru/post/268239/


All Articles