keeps21 Posted June 5, 2009 Share Posted June 5, 2009 I have a FrontController class which takes a request, explodes it into an array, the first item being the controller, second being the action and then the 3rd is the querystring. It then loads the correct controller and it's method. For example this - http://mysite.com/news/view/?id=21 Would load the news controller and view action and the querystring would be id=21 Here is the class code class FrontController { private static $instance; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } function __construct() { } function processRequest($request) { # Set default request if blank if ($request == '') { $request = 'index/index'; } # Create urlArray and populate with request data $urlArray = array(); $urlArray = explode("/",$request); # Get controller from request $controller = $urlArray[0]; # Move onto action from request array_shift($urlArray); if (array_key_exists(0,$urlArray)) { $action = $urlArray[0]; } else { $action = 'index'; } # Move onto querystring from request array_shift($urlArray); $queryString = $urlArray; # Set controller name - append Controller to name $this->controllerName = $controller; $this->actionName = $action; $controller = $controller; $controller .= 'Controller'; $action .= 'Action'; # Create instance of requested controller $dispatch = new $controller($this->controllerName,$action); # Call requested method on invoked controller try { if (!(int)method_exists($controller, $action)) { Throw New Exception('Method - ' . $action . ' - does not exist.'); } else { call_user_func_array(array($dispatch,$action),$queryString); } } catch (Exception $e) { echo 'System Error: ' . $e; } } function __destruct() { } } This code is modified a little from some I found on the internet (http://anantgarg.com/) That all works fine, the correct controller and action is loaded. The problem I'm having is with this bit call_user_func_array(array($dispatch,$action),$queryString); How do I access the $queryString variable in my controller, where am I passing it to with the above code? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted June 5, 2009 Share Posted June 5, 2009 It is passed in as the first parameter in whatever controller function is called Here is your controller object $dispatch = new $controller($this->controllerName,$action); This is the classname $controller This will pass the query string as a parameter into the controller method call_user_func_array(array($dispatch,$action),$queryString); Quote Link to comment Share on other sites More sharing options...
keeps21 Posted June 5, 2009 Author Share Posted June 5, 2009 So say I've loaded my IndexController I should be able to access it in the IndexController with $this->queryString; Or am I missing something? Quote Link to comment Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 You have methods inside other methods in your class, quite strange... Quote Link to comment Share on other sites More sharing options...
keeps21 Posted June 5, 2009 Author Share Posted June 5, 2009 It's my 'take' on a simple framework. FrontController takes a request and loads the requested ActionController and it's method. I want to pass the query string that is defined in the FrontController to the ActionController which is executed by the processRequest() method in the FrontController Quote Link to comment Share on other sites More sharing options...
keeps21 Posted June 5, 2009 Author Share Posted June 5, 2009 I didn't actually need to use the function call_user_func_array() - changed it to $dispatch->$action; and now passing $queryString to my controller when it is constructed meaning I can access $queryString at $this->queryString in my controllers. Here is the full code if anyone is interested <?php class FrontController { private static $instance; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } private function __construct() { } function processRequest($request) { # Create urlArray and populate with request data $urlArray = array(); $urlArray = explode("/",$request); # Get controller from request $controller = $urlArray[0]; # Move onto action from request array_shift($urlArray); if (array_key_exists(0,$urlArray)) { $action = $urlArray[0]; } else { $action = 'index'; } # Move onto querystring from request array_shift($urlArray); $queryString = $urlArray; # Set controller name - append Controller to name $this->controllerName = $controller; $this->actionName = $action; $controller = $controller; $controller .= 'Controller'; $action .= 'Action'; # Create instance of requested controller $dispatch = new $controller($this->controllerName, $action, $queryString); # Call requested method on invoked controller try { if (!(int)method_exists($controller, $action)) { Throw New Exception('Method - ' . $action . ' - does not exist.'); } else { $dispatch->$action(); } } catch (Exception $e) { if (ini_get('display_errors') == 1) { echo 'System Error: ' . $e; } } } private function __clone() { } function __destruct() { } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.