I am working on a project using HMVC codeigniter. i have my own library for layouts and my base controller named MY_Controller with this code:
class MY_Controller extends MX_Controller{
public $layout='layout_name';
public $theme='name';
...
public function __construct(){
parent::__construct();
...
}
}
my problem is:
when i use $this->CI=& get_instance(); in the library, the properties of my controller like $theme are unavailable. for example can not use $this->CI->theme; but in the controllers no problem exist and i can use $this->theme; anywhere. how can i make an instance contain my base controller variables in the library?
Solved. according to the CI documentation, i use second parameter of loading library and send my specified variables to library via array. in the mx_controller:
class MY_Controller extends MX_Controller{
private $layout='default';
private $theme='white';
private $param=array();
public function __construct(){
parent::__construct();
$this->param=array('layout'=>$this->layout,'theme'=>$this->theme);
$this->load->library('lib',$this->param);
}
}
Related
I want to access a function on application/config/constants.php, which is written in one of my CodeIgniter model. Is it possible to do it?
There is no reason to change a constant with a model function or any function for that matter. Constants are meant to be static and strict definitions. Like application paths, version numbers, .etc.
They are similar to a variable except that they can never be changed.
If you wish to have a variable that is a superglobal but needs to be dynamic in some respect you can create a /application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public $someglobalvar;
public function __construct() {
parent::__construct();
$this->load->model('somemodel');
$this->someglobalvar = $this->somemodel->get_var();
}
}
and have your controllers extend it instead of CI_Controller
(application/controllers/Some_controller.php)
class Some_controller extends MY_Controller {
public function index() {
var_dump($this->someglobalvar);
}
}
I have a relatively simple question. I am trying to inherit a constructor from a php superclass to authenticate on this controller.
Here is my super class:
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
}
}
and here is my subclass:
class Event_Controller extends Auth_Controller {
function __construct()
{
parent::__construct();
}
public function get_events_by_owner() {
$this->load->model('Event_model');
$data['events'] = $this->Event_model->select_by_owner($_SESSION['SignedIn']);
$this->load->view('event_view', $data);
}
}
This is not working. only a white page is rendered. I'm not sure why it isn't working. If I move the constructor from Auth_Controller to Event_Model this works.
Thanks!
EDIT:
Fatal error: Class 'Auth_Controller' not found in
../controllers/event_controller.php on line 12
Your solution is not going to work, you should try either:
Move this code:
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
to a cutom library, than run this library within constructor of your controller. Please read Creating Libraries for details.
or:
Create MY_Controller class and put auth code (quoted above) into its constructor. Than you'll be able to extend like:
class Event_Controller extends MY_Controller {
(...)
Please read Creating Core System Classes for details.
One stupid question, I want to call from my admin_news controller function which resides in another controller Admin. FUnction name is is_logged_in();
admin.php
public function is_logged_in()
{
....
}
admin_news.php
public function __contruct()
{
parent::__construct();
//admin->is_logged_in();??
}
how can I do that?
Thanks
You will have to move that functionality somewhere else, Codeigniter's architecture doesn't allow multiple controller instances in one request. You have multiple options like using a common base class, libraries, helpers and so on.
I would recommend you to create your own MY_Controller base class (see Extending Core Classes) and put your method there, like this:
class MY_Controller extends CI_Controller {
protected function is_logged_in() {
// ...
}
}
Once you have it there you can call it like:
class AdminNews extends MY_Controller {
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
}
I'm using CodeIgniter and I want to create some sort of filter to know when a user may/may not access a current controller
If anyone has any idea how to implement this in a different manner, great, but what I was thinking (and trying to do) is the following:
CI_Controller - which is the basic CodeIgniter controller class
MY_Controller - the basic controller which I use which extends CI_Controller
[Controller] - any "physical" controller
what i've tried to do is:
MY_Controller.php
class MY_Controller extends CI_Controller{
private static $namespace = null;
private static $permission = array('site', 'settings');
public function __construct(){
if ((!isset($_SESSION['user'])) && (in_array(__CLASS__, $permission))){
throw new Exception('Unauthorized');
}
parent::__construct();
}
}
obviously, this doesn't work as CLASS will always be that of MY_Controller and not that of the child object... and NAMESPACE doesn't work aswell.
Anyone has any idea? because Id really hate to start putting this snippet of code in every other class, and I'll prolly need the filtering later for some more elaborate things...
As there's no answer so far, maybe this suggestion might help. It's a different approach, though, that uses the Modular Extensions - HMVC ...
Like this you can have a module "login" with a controller that holds some methods to log a user in, to check session status and to log a user out (and the like).
In each other module you can now load the module login and check for the status and redirect if needed ...
There's a tutorial HMVC: an Introduction and Application I followed on nettuts that shows how to do the CodeIgniter From Scratch: Day 6 – Login in that way. Maybe that helps. I had some difficulties as it's with an old codeigniter version. So maybe this thread helps.
It's not how you're trying to do things, but maybe it helps!
I did not try it but what I saw in this forum might be working for you. The predicted code is:
class MY_Controller extends Controller{
private static $permission = array('site', 'settings');
public function __construct(){
if ((!isset($_SESSION['user'])) && (in_array(__CLASS__, $permission))){
throw new Exception('Unauthorized');
}
parent::Controller();
}
}
But then you'd need to use
class My_controller extends MY_Controller
instead of
class My_controller extends Controller
I solved it in the following manner... simply in the constructor, I defined the current class
class MY_Controller extends CI_Controller{
private static $permission = array('site', 'settings');
public function __construct($currentController = __CLASS__){
if ((!isset($_SESSION['user'])) && (in_array($currentController , $permission))){
throw new Exception('Unauthorized');
}
parent::Controller();
}
}
when calling the physical controller, we simply write as follows
class PhysicalController extends MY_Controller{
public function __construct(){
parent::__construct(__CLASS__);
}
}
I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.
My 3 classes:
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
// application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
// application/controllers/user.php
class User extends Public_Controller{
public function __construct(){
parent::__construct();
}
}
Fatal error: Class 'Public_Controller' not found in /srv/www/xxx/application/controllers/user.php on line 2
Curious is that the following snippet is working, if I directly extends from MY_Controller:
// application/controllers/user.php
class User extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I have loaded the controllers via __autoload() or manually. The controllers are loaded succesfully.
CI-Version: 1.7.3
You need to require the Public Controller in your MY_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php');
You get the error because Public_Controller was never loaded. Doing this would allow you to extend from Public_Controller
I like what you are doing because I do that all the time.
You can do this also in your MY_Controller when you want to create an Admin_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php'); // contains some logic applicable only to `public` controllers
require(APPPATH.'libraries/Admin_Controller.php'); // contains some logic applicable only to `admin` controllers
You should place Public_controller in with MY_Controller inside MY_Controller.php
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I use __construct everywhere and it works fine I recently wrote up an article on how to do this in relation to wrapping your auth logic into your extended controllers. It's about half way down when I start discussing constructing your controllers.
Problem was solved here: http://devcrap.net/pl/2011/09/04/codeigniter-dziedziczenie-z-my_controller-extends-my_controller/. In polish but code is good :]
I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching].
Note:- In localhost I didnt have any error.
In extended controller I Use
class Home extends MY_Controller{
function __construct() {
parent::__construct();
}
}
even I got the error.
After changing my file name to MY_Controller it started to work well.
I have a custom controller class called MY_Controller it extends CI_Controller and it works fine. It is located at application/core and it has custom functions lo load views in my site.
I use an abstract class My_app_controller that extends MY_Controller for my_app specific behabior, I want every controller in my_app to extend this abstract class. (I use diferent apps in the site, so some apps will extend My_app_controller and other apps will extend My_other_apps_controllers)
But when I try to extend My_app_controller from any controller in my application, "Main_app_controller extends My_app_controller" generates a Class 'My_app_controller' not found exception.
I found two solutions:
use include_once in Main_app_controller.php file.
include_once APPPATH.'controllers/path/to/My_app_controler.php';
break the "one class per file" rule of code igniter and define my My_app_controller just in the same file MY_Controller is (under application/core).
Manual says:
Use separate files for each class, unless the classes are closely
related
Well... they are.
Anyway, I prefered to use the include_once solution as I think it is better to have one file per class and My_app_controller is located under application/controllers/my_app folder. (so application/controllers/other_apps will exist)