default language codeigniter - codeigniter

i want to run this code at the first of all my webpages,
I'm using codeigniter
the code is:
if(!$this->session->userdata('lang')):
$this->session->set_userdata('lang','ar');
endif;
how to do that?

If you haven't already, it may be worth looking at CodeIgniter's language class.
You can extend the native CI_Controller class. Extending this class allows you to add your additional functionality, while the original functionality of the native core class remains.
For the code to run first on all pages, you can add your code to the constructor of the newly created subclass - if you extend your controller(s) with the new subclass, then this code will be run when any function in your controller(s) is called. To do this:
Create a subclass
Create a file named MY_Controller.php in the application/core/ directory of your project. This new class needs to extend CI_Controller and the parent constructor. The class should look something like this:
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct(); //make sure you extend the parent constructor
//Your code:
if( ! $this->session->userdata('lang') )
$this->session->set_userdata('lang','ar');
}
}
Ensure the class prefix is correct
If you've used MY_ as the prefix for the new class then you shouldn't need to do anything here, but it's useful to know anyway.
You'll also need to ensure that the sub-class prefix is set correctly in the application/config/config.php file.
$config['subclass_prefix'] = 'MY_';
This prefix must match the prefix of the new class that you create. By default it is MY_ but you can change it to what you want as long as they correspond - FOO_, BAR_, WHATEVER_... The exception is CI_, which is reserved for CodeIgniter's native libraries.
Extend all of you application's controller with the new subclass
Your controller(s) in application/controllers/ is(/are) probably extending CI_Controller. To make use of the newly created subclass, your controller should extend MY_Controller and the parent constructor.
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
//More functions...
}

Related

Is there a way of initializing a codeigniter 4 Model variable once in a Controller

I understand that we could autoload or load models in the constructor and use them all through the class in earlier versions of codeigniter like below:
//Declaring the variable
$this->load->model('model_name');
//Using it through the controller
$this->model_name->function();
But in codeigniter 4, is there an easier way of defining a model variable once in a Controller. The current way i know of doing that is :
//Declaring the variable
$model_var = new ModelName();
//Using it through the controller
????????
But as far as i have tried looking, i have to do the initializing in every function.
The easiest thing to do is create a property for the controller to hold an instance of the model.
CI v4 relies on an initializer function instead of a constructor to set up properties and the like. So creating something like this would follow "best practice" for a simple controller in a simple app.
<?php
namespace App\Controllers;
/**
* Class SomeController
*
*/
use CodeIgniter\Controller;
class SomeController extends Controller
{
/**
* #var ModelName instance
*/
// $modelVar; ```edited - will throw a php syntax error```
protected $modelVar; ```this works```
/**
* Initializer
*/
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
// Load the model
$this->modelVar = new ModelName();
}
}
For your convenience, CI supplies /app/Controllers/BaseController.php that the above example used in part.
The idea behind BaseController was to provide a class that could be used to load resources (classes, helpers, etc) that EVERY controller in the application would need. Then each controller would extend from BaseController. The above example assumes that there isn't a need for anything else, which in real-life is unrealistic. So the above could extend BaseController too if there were a need.

Automatically call a function when inside a certain module/controller

Is it possible to call a function in a certain module every request?
Let say I have module name called 'configuration', on this module, I have a list of controllers and list of functions/methods. What I want is to automatically pass my "Menu" to the View without manually passing it on each methods and controllers.
This menu is only available when inside the 'configuration module.
// I have extended the base controller to create common functions
ConfigureController extends \BaseController
{
protected function processMenu() {
}
}
// One of my controller that needs to render processMenu()
SetupController extends ConfigureController
{
public index()
{
// I want to optimize this portion so that I do not have to call it evertime
$pass_to_view = $this->processMenu();
// I need to pass it again and again
return View::make('setup')->with('data', $pass_to_view );
}
}
PS. sample code only
Thank you in advance!
Use the BaseController constructor method __construct() and within the SetupController's constructor call parent::__construct();
This is where the view composers come handy.
Put your menu in a partial, include it in you layout, then register a view composer (doc here: http://laravel.com/docs/4.2/responses#view-composers).
You can put your register code anywhere, for instance you could create a file composers.php in app and include it in your app/start/global.php.

Access an object of one class in other codeigniter

I have two controller classes in my codeigniter application, say class A and B.I just want to create an object of class A and access the functions declared in class A from class B.Something like:-
class A extends someclass
{
public function function1(){
$this->load->view('welcome_message');
}
}
}
class B extends someclass2
{
protected $object;
public function __construct()
{
parent::__construct();
$this->objectA = new A();
}
}
}
I want to access the function function1 from class B using the object objectA. How can i do this?
Please help.
Thanks
well actually this is not the proper way in codeigniter. Actually when you have common functions in and you want to use them in 2 or more controllers. The best way is to create base controller in core folder with name of MY_Contoller and extend it from CI_Contoller. Write your common function in MY_Contoller. Now you have to extend all your controllers from MY_Contoller instead of CI_Contoller. You can do the same with Model.
Cross-controller access goes against CI best practice.
Either inherit both controllers from a controller that holds this common functionality (don't forget to prefix the function with '_' so it's inaccessible via url routing) or create a library that contains your re-usable functionality. A helper can also work.

Extend CodeIgniter Base Model causes Fatal error: Class 'MY_Model' not found

I want to extend CI_Model:
<?php
class ModelBasic extends CI_Model
{
}
?>
It won't be autoloaded unless under such a path:
./application/core/MY_Model.php
It seems name of MY_Model.php is mandatory. Now my problem is that when I create MY_Model.php CodeIgniter Expects me to have a model under name of MY_Model which I want to avoid. Can I have my own custom model name without making a fake class to suppress this error?
Fatal error: Class 'MY_Model' not found in /var/www/CodeIgniter/system/core/Common.php on line 174
The quickest way to achieve naming your own custom base model(s) without any additional modification is to create the file application/core/MY_Model.php, define an empty MY_Model class, and then create your own custom class(es):
class MY_Model extends CI_Model {}
class ModelBasic extends MY_Model {
// Your code here
}
// Define more than one if you want.
class ModelComplicated extends MY_Model {
// Your code here
}
Other options include:
Extend the Loader class, and replace the model() method with your own modified version. This could be complicated (I haven't explored it fully), since the actual error is occurring in system/core/Common.php :: load_class(), which, in the model's example, is instantiating the base CI_Model class, as well as MY_Model if it finds one (which it automatically looks for by default, like other base classes).
Create / Add an autoloader that follows your own rules for loading core classes (may be complicated if you try to autoload more than just models -- really depends on how you want to set your app up).
Extend the Loader class and re-write that part of the code.

How to auto include to use own class with CodeIgniter

I am using CodeIgniter. I want to use my own class to pass as an argument inside controller functions.
Normally, I can put this class in a folder and include it to MY_Controller with its path. But I want to learn if there is a way to do this in CodeIgniter. I can't put it in libraries folder and can't use loader class because it tries to create an instance of an object, but I want to create instance whenever I want. Loader class gives an error if my own class need constructor parameters.
What is the best way to do that?
Which is the best folder to put in it?
This is very easy. Consider this example
<?php
Class Home extends CI_Controller{
public $arg1 = 1;
public $arg2 = 2;
function index($this->$arg1 , $this->$arg2){ //or function index()
//Then inside function
//$vara = $this->$arg1 , $varb = $this->$arg2;
}
}

Resources