CodeIgniter call protected method of another controller - php

I have two classes on my Codeigniter project: Users and Profiles
Users:
class Users extends CI_Controller
{
...
Profiles:
class Profiles extends CI_Controller
{
protected function create_user_profile()
{
....
}
...
When a user is created by user controller, a profile should be created immediately. So a function in Users has to call create_user_profile function. Now my question is:
If I make create_user_profile public, one can call it by URL. But if I keep it protected, then how to call it from User controller?
Is there a better way than moving create_user_profile from Profiles controller to Users controller?

Try making a Profiles Library instead :
Libraries/profiles.php
class Profiles
{
protected $CI;
public function __construct()
{
$this->CI =& get_instance(); // Existing Code Igniter Instance
}
public function create_user_profile()
{
// Your Code Here
// can communicate back with CI by using $this->CI
// $this->CI->load->view(....);
// $this->CI->load->model(...);
// ETC
}
}
controllers/users.php
class Users extends CI_Controller
{
public function my_function(){
$this->load->library('profiles');
$this->profiles->create_user_profile();
}
}

Put it in a different, non-web-controller class. Then you can re-use it from everywhere.

Related

Method Calling in Codeigniter 3 from two different controllers

I am using CodeIgniter 3.
Here's my Admin controller where I insert a record into the table people:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
This is another controller where I perform the same operation as the Admin controller, but it writes the same code twice. I want to reuse the code in the Employee controller:
class Employee extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
Another way I found is to redirect(../admin/addpeole) in Employee, but it changes the URL.
Is there any another way to reuse same code in the Admin controller in the Employee controller without changing the URL?
Controllers should not communicate with the database directly, You should be using Models to communicate with the database, And your Controllers should be communicating with models then sending the output either directly or through a view.
You can create a model under application/models You'll have to give it a name that does not conflict with controllers so most people add the suffix _model or _m
e.g: application/models/Employees_model.php
it should extends CI_Model the code would be something like this.
class Employees_model extends CI_Model {
public function add_employee($data) {
// insert code goes here
}
]
then in your controller you load the model & send it the $data to create an employee
class Employees extends CI_Controller {
public function create () {
$this->load->model('employees_model');
$this->employees_model->add_employee($this->input->post());
}
}
try this
In core create a file called MY_Controller.php and extend from CI_Controller.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
?>
Write the function in that controller
Extend Employees form the controller created in core.
class Employee extends Admin_controller{
public function __construct() {
parent::__construct();
}
function abc() {
$this->addpeople();// this will insert the values for you
}
}

Access an array in all views in codeigniter

I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.

is_logged_in check on every page

I am using CodeIgniter. I have a controller named abc and i has functions named index,a,bandc.
www.example.com/abc/
I want that user can only access the area he is logged in.
www.example.com/abc/ //if loggged in else back to homepage
or
www.example.com/abc/a/ //if loggged in else back to homepage
Now to check login. I use:
if($this->auth->is_logged_in()) { .. } else { redirect('/'); }
on every function individually.
Is there any other way to do so ??
I think you can do this by overriding the constructor and call your function in it.
<?php
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
// check login
}
}
?>
For a particular controller you can put your if check in the constructor of the controller so that when any method of the controller is called it will pass through your if check
class Abc extends CI_Controller {
public function __construct()
{
parent::__construct();
//your if goes here
}
}
And if you want to check the user is logged in or not in the whole application you can use the constructor method __construct() of CI_Controller so it will be validated when user access any of the controllers within your application
class CI_Controller {
private static $instance;
/**
* Constructor
*/
public function __construct()
{
//your if goes here
}
}

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php

Codeigniter login to controller

Hey! I'm very new to Codeigniter, I'm trying to protect the entire admin controller. I figured I'd start here:
function Admin()
{
parent::Controller();
if(!isset($_SESSION['loggedin'])){
$this->login();
}
}
but this is obviously incomplete. How do I also stop the method that is trying to run ( ie index() ), and am I on the right track here??
Thanks for your help!!
there is
Extend the base controllers:
MY_Controller.php
<?php
class MY_Controller extends Controller {
function __construct()
{
parent::Controller();
$user_id = $this->session->userdata('user_id');
$this->data['user'] = $this->user_lib->get($user_id);
}
}
?>
you can store all kinds of info in this construct. This just gets the currently logged in users ID and assigns it the $data['user'] . This will be adjusted depending on which sort of auth library you use but you get the gist. You now have access to the current users ID, and all their details, from within any controller that extends "MY_Controller"
now you can create an "admin" controller, or any number of other ones to restrict access. like so:
Admin_Controller.php
<?php
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'admin')
{
show_error('Error - you need to be an admin.');
}
}
}
?>
Public_controller.php
<?php
class Public_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'member')
{
show_error('You need to login to see this page...');
}
}
}
?>
as you can see..possibilities are endless
So, for admin only pages - use the admin controller
for member only pages - public
for "normal" pages - use the default controller.
I'll link to Phil Sturgeon's article as it's where I read about it first
put the checking session code in every function in Admin Controller that you want to protect.
that is the easiest way to do it..

Categories