Codeigniter: session class in routes.php - php

How can I access the codeigniter session class inside routes.php?
I need that class to route all the request (except /login) to a certain controller, unless the user has admin privileges ($this->session->userdata('logged')).
All the route rules are working, I only need to access that class.

You are not typically able to access the Singleton ($this->) from the config & routes file because the classes are not loaded at that point
although there are a few workarounds to access the session, the better way would be to a MY_Controller & the _remap() function:
http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping
Here's some sample code that explains a little more about how they work:
http://www.codebyjeff.com/blog/2012/11/ci-_remap-function-the-friend-you-never-knew-you-had

Create this MY_Controllerand store it in application/core/, then have your other controllers extend it:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->_check_auth();
}
private function _check_auth(){
if(!$this->session->userdata('is_admin')){
$this->session->sess_destroy();
redirect('login');
}
}
}
Note: above code assumes you already have a user login system in place.

Related

Called controller can't access anything

in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}

Codeigniter - Extending my own controllers

I'd like to know if it's possible to extend my own controllers. I've been working on web-based applications for a while and I'm now starting to find each customer that wishes to use the application has different requirements of how it should work. My thoughts are that if I generate a base structure and then extend the controllers to override any of the functions that the customers require to work differently.
First of all, could you tell me if I'm on the correct track, and secondly, how do I go about extending my own controllers (if I can)? I've tried the usual:
Class Reports2 extends Reports { }
This doesn't work but I'm guessing it has something to do with the location of the file I'm trying to extend. My file structure is as follows:
Application
--->controllers
-------->control_panel
------------>reports.php
If I am not mistaken then you should be able to easily do this:
reports2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/control_panel/reports.php');
Class Reports2 extends Reports {
public function __construct(){
parent::_construct();
}
public function index(){
}
}

Codeigniter -- working with multiple controllers

I have a question which regards to controllers. Let's get it started:
I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.
So for this example:
I have 3 controllers: admin.php , company.php, usermanagement.php
What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement
I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.
How would I achieve this?
by the way here's a snippet of my code:
admin.php - Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_admin');
}
}
Company - Same as the admin
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_company');
}
}
Thanks,
James
EDIT: I have tried adding functions on the admin.php like:
function company() {}
function usermanagement() {}
but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.
If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:
class Company extends CI_Controller {
public function __construct() {
parent::__construct();
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(3));
}
}
...
Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.
You can add more functions in your 'admin' controller, so, default page is:
public function index() {
$this->load->view('view_admin');
}
'Subpage' is:
public function company() {
$this->load->view('view_company');
}
etc, etc...

What to do when php session does not continue in another controller in codeigniter?

I'm developing a web app using codeigniter but I prefer to shy off from handling sessions the codeigniter way due to the bugs it's associated with.Therefore I'm using native php session handling. The problem I'm having is that it seems the session which is started from the default controller doesn't continue in other controllers and it's a pain in the neck.
In both my default controller and the other controllers, I've started or continued the session way on top, before any other code like this:
<?php
#session_start();
So what do I do to make the session continue in the other controllers instead of being confined in the default controller?
As all the controllers get loaded via the main index.php file, you could start the session in there
Create a MY_Controller.php in the core folder with the session start call in its construct - set this to extend CI_Controller and make your other controllers extend your newly created MY_Controller instead.
Something like:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
#session_start();
}
}
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories