CodeIgniter Forums

Full Version: Controller within Controller Issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey,

In CodeIgniter 2.2.0 I was able to create a Controller object inside a Controller. In CodeIgniter 3.0.0-develop I am no longer able to do this because I get the error message: "Unable to locate the specified class: Session.php". Am I missing something? Here is my setup.

1) autoloader.php:
PHP Code:
$autoload['drivers'] = array(
 
   'session'
); 

2) config.php:
PHP Code:
$config['sess_driver']            = 'native';
$config['sess_valid_drivers']    = array();
$config['sess_cookie_name']        = 'bbp_user_sessions'// Custom class.
$config['sess_expiration']        = 21600              // 6 hours
$config['sess_expire_on_close']    = FALSE;
$config['sess_encrypt_cookie']    = TRUE               // Use security
$config['sess_use_database']    = TRUE               // Use database
$config['sess_table_name']        = 'bbp_user_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']    = 300

3) bbp_user_sessions table
Code:
CREATE TABLE `bbp_user_sessions` (
 `session_id` varchar(40) NOT NULL DEFAULT '0',
 `ip_address` varchar(45) NOT NULL DEFAULT '0',
 `user_agent` varchar(120) NOT NULL,
 `last_activity` int(10) unsigned NOT NULL DEFAULT '0',
 `user_data` text NOT NULL,
 PRIMARY KEY (`session_id`),
 KEY `last_activity_idx` (`last_activity`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Can you post your controller example too.
(01-13-2015, 09:21 AM)Rufnex Wrote: [ -> ]Can you post your controller example too.

My example is too complex, so I replicated the very same problem using the CodeIgniter-Develop base.

Step 1) Download 'CodeIgniter-Develop'

Step 2) Update  autoloader.php & config.php & bbp_user_sessions table so it looks like my initial post.

Step 3) Don't forget to set the encryption key in config.php too. I used:
PHP Code:
$config['encryption_key'] = 'Gargantia on the Verdurous Planet'

Step 4) Modify the 'Welcome.php' file that comes already with the framework to look like this:
PHP Code:
<?php
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @package    CodeIgniter
 * @author    EllisLab Dev Team
 * @copyright    Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
 * @copyright    Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
 * @license    http://opensource.org/licenses/MIT    MIT License
 * @link    http://codeigniter.com
 * @since    Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

    
/**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    
public function index()
    {
 
       $this->_load_home_controller();
 
       
        $this
->load->view('welcome_message');
    }
 
   
    private 
function _load_home_controller()
 
   {
 
       include(APPPATH.'controllers/Home.php');
 
       $controller = new Home();
 
       $controller->index();
 
   }
 
   
}

/* End of file welcome.php */
/* Location: ./application/controllers/Welcome.php */ 

Step 5) Create a 'home.php' controller and fill it with the following contents:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Home extends CI_Controller {

    
/**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    
public function index()
    {
 
       echo "Testing: Home Page";
 
       
        $this
->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/Welcome.php */ 


Step 6) Load up webapp through your Nginx/Apache/Lighttpd/etc and you'll see the same error message.
I see .. the problem is, that the session is loaded twice .. for the main controller and also from the included controller.
Right now i have no idea how to solve this.
I logged a ticket for this issue: https://github.com/bcit-ci/CodeIgniter/issues/3480
you can't call a controller inside another controller. this is not hmvc (or whatever the name is...)
Quote:In CodeIgniter 2.2.0 I was able to create a Controller object inside a Controller.

Yeah, don't do that. It's a really bad anti-pattern for a framework like CodeIgniter.

Instead, you may want to move the relevant code in the "recipient controller" into a model instead. There is a one-to-one relationship between an HTTP request and a controller, but a one-to-many between controllers and models.

This may change in future versions, but for 2.2 and 3.0-develop as currently implemented, that's how it is.
(01-14-2015, 08:27 AM)sarciszewski Wrote: [ -> ]
Quote:In CodeIgniter 2.2.0 I was able to create a Controller object inside a Controller.

Yeah, don't do that. It's a really bad anti-pattern for a framework like CodeIgniter.

Instead, you may want to move the relevant code in the "recipient controller" into a model instead. There is a one-to-one relationship between an HTTP request and a controller, but a one-to-many between controllers and models.

This may change in future versions, but for 2.2 and 3.0-develop as currently implemented, that's how it is.

Hi I am working with Hideauze on this project. I understand where you are coming from, it's just that it worked in 2.2 and we really want to take advantage of what 3.0 has to offer.

It looks like we've overcome most of these issues - the last thing we are having a problem with is wide Ajax calls.

We could leave our project in 2.2, but from a security and overall standpoint I'd really like to see this in 3.0.

I realize that HMVC isn't supported, but we've gotten it so close, that I'm wondering if any insights can be provided.

If anyone wants to take a stab at this, I'm glad to share some code.
(02-04-2015, 02:17 PM)rodolfomartinez Wrote: [ -> ]
(01-14-2015, 08:27 AM)sarciszewski Wrote: [ -> ]
Quote:In CodeIgniter 2.2.0 I was able to create a Controller object inside a Controller.

Yeah, don't do that. It's a really bad anti-pattern for a framework like CodeIgniter.

Instead, you may want to move the relevant code in the "recipient controller" into a model instead. There is a one-to-one relationship between an HTTP request and a controller, but a one-to-many between controllers and models.

This may change in future versions, but for 2.2 and 3.0-develop as currently implemented, that's how it is.

Hi I am working with Hideauze on this project. I understand where you are coming from, it's just that it worked in 2.2 and we really want to take advantage of what 3.0 has to offer.  

It looks like we've overcome most of these issues - the last thing we are having a problem with is wide Ajax calls.

We could leave our project in 2.2, but from a security and overall standpoint I'd really like to see this in 3.0.

I realize that HMVC isn't supported, but we've gotten it so close, that I'm wondering if any insights can be provided.

If anyone wants to take a stab at this, I'm glad to share some code.

You can always post in the "General Help" forum, just be more specific about it.
Check this replacement for the Session driver and tell me if it works for you:

https://github.com/cgarciagl/CI3-Native--Session

Cheers
Pages: 1 2