Jump to content

[SOLVED] call_user_func_array() and FrnotController class help


keeps21

Recommended Posts

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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() {
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.