Welcome Guest, Not a member yet? Register   Sign In
call controller from another one
#1

[eluser]EugeneS[/eluser]
i know this topic was here several times but we still dont have any solution.
i lso know this way is against mvc specification Smile


so i need to receive all controller methods from another controller.

lets imagine i have controller and i need to create an object from another controller to use get_class_methods function ... smth like this (just to show you an idea):

Code:
class ScanModules extends Controller {

    var $scanpath;

    function ScanModules()
    {
        parent::Controller();    

        $this->load->library('common');
        $this->common->Visitor();
        $this->scanpath = './system/application/controllers/';

        ........
        ........
        ........

        $file_name = 'award.php';
        $this->_readClass($file_name);
    }


    function _readClass($file_name, $directory = NULL)
    {
        if (preg_match("/([a-zA-Z\-0-9]*?)\.php/i", $file_name, $class_name))
        {
            $class_name = $class_name[1];
            include($this->scanpath.$directory.$file_name);
            $tmp_class = new $class_name;
        }
    
    }

........
}

so when i call $tmp_class = new $class_name; i receive error:
(sure award.php is usual CI controller which load libraries, helpers etc and which is also extends "controller" .... so how to be ?)

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: About::$mItemCategory

Filename: libraries/Loader.php

Line Number: 921

Fatal error: Call to a member function _assign_libraries() on a non-object in C:\.....\system\libraries\Loader.php on line 921


so how to be ?
#2

[eluser]wiredesignz[/eluser]
why bother, there are easier ways.
#3

[eluser]EugeneS[/eluser]
[quote author="wiredesignz" date="1196446696"]why bother, there are easier ways.[/quote]

any suggestions ?
#4

[eluser]EugeneS[/eluser]
so any one knows how to get list of methods of the controllers from another controller ?
#5

[eluser]Majd Taby[/eluser]
http://www.php.net/manual/en/function.method-exists.php
#6

[eluser]wiredesignz[/eluser]
[quote author="EugeneS" date="1196452212"][quote author="wiredesignz" date="1196446696"]why bother, there are easier ways.[/quote]

any suggestions ?[/quote]


Use an autoload library then functions are available to all Controllers.

Use a Model if the functions include domain logic.

Use a Base controller with functions and extend Page Controllers off that.
#7

[eluser]esra[/eluser]
If your interest is in sharing methods across multiple controllers, more approaches are available if you are using PHP5. For example, you can create sharable libraries of methods rather than placing the code for those methods in a controller and call those methods using intermediate controller methods based on the factory design pattern. The factory pattern can be implemented in controller methods or separate factory classes can be created to call the various library methods. A factory delegates the resposibility for performing an action to a library containing the appropriate method. For some PHP sample code on the design pattern itself, try here:

http://www.fluffycat.com/PHP-Design-Patt...ry-Method/

The factory method promotes polymorphism, allowing the same objects to be shared across an application.

If you want controllers to communicate in the interest of sharing methods or for object synchronization reasons (e.g., automatically updating an input element after making a selection from a list or grid), the MVC architectural pattern might not be the best solution for you. In this case, you might research PAC or HMVC.
#8

[eluser]wiredesignz[/eluser]
The guy needs to understand basic MVC before he can get to all that.
#9

[eluser]xwero[/eluser]
Wiredesignz please take it easy. There are legitimate uses for calling controllers in controllers like for instance in a modulated site. Modules don't always have to be aware of the general lay-out or other user actions.

Like esra pointed out there are other design patterns which aren't used in the CI framework but have al lot going for them like the HMVC pattern.

The limitations of MVC can result in jack-of-all-trades libraries and model like controller hacks, is that a good way to go?
#10

[eluser]EugeneS[/eluser]
[quote author="wiredesignz" date="1196529123"]The guy needs to understand basic MVC he can get to all that.[/quote]

OHHHH MY GOD, this flame does me crazy.

MAN i know MVC pattern at least not worse than you ....
READ CAREFULLY WHY I NEED THIS ... IF YOU CAN PROPOSE ANOTHER WAY THEN PLEASE DO IT, but stop this flame please about "read the MVC pattern" "go and read php manual" etc ...

CAN YOU SUGGEST HOW CAN I RECEIVE ALL THE METHODS OF THE for example 20 CONTROLLERS at once by means of CI without just parsing php files with reg.exps. ?


the only way i see is to use "get_class_methods", but as i said before i cant create an object from another controller to use it in this function because that controller uses libraries, helpers etc...

IF YOU DONT HAVE ANY IDEAS PLEASE GO AND DO YOUR "HELLO WORLD PAGES".




Theme © iAndrew 2016 - Forum software by © MyBB