2

I'm creating my own framework. It works like this localhost/controller/action/firstVariable/second/third (And so on...) My bootstrap look like this:

$request        = Util::getInput('request');
$requestList    = explode("/",$request);
$modelName      = @ucwords($requestList[0]);
$action         = @$requestList[1];
$parameters = array_slice($requestList,2);
$controllerName = $modelName.'s'.'Controller';

I'm getting parameters from an url and save them in a variable $parameters. I would like to send them to the current action in my controller the way Laravel 5 is doing.

Example, in Laravel I specify parameters in the url and thats it.

To call them, I need to do a simple step. Just define them:

public function firstAction($first,$second){

}

When I go to an url like: localhost/Main/firstAction/first/second/

Function of action 'firstAction' will catch those 2 parameters and then basically I can call them inside of the controller and send it to view.

My extends Controller class:

class Controller{
public function __construct($model,$action){
    $modelClass = new main();
    $reflection = new ReflectionClass($model.'sController');
    $reflection->hasMethod($action) ? $this->$action() : die ('Base Controller call error: Method '. $action .' does not exist in Controller '. $model.'sController');

}
public static function renderView($action,$model,$data){
    $model = str_replace('sController','',$model);
    //include '../application/views/'.$model.'/'.$action.'.php';
    $loader = new Twig_Loader_Filesystem('../application/views/'.$model);
    $twig = new Twig_Environment($loader);
    echo $twig->render($action.'.php', $data);
}

}

class MainsController extends Controller {

private $_data = array();

public function __construct($model,$action){
    parent::__construct($model,$action);
}

public function firstAction($first,$second){
    echo 'Hoi';
}

}

How can I do it, the good way? I can of course send the variable $parameter to MainController and than call $this->_data inside of my action but It is not efficient. I think I need to use arrays to do it, but I have no idea how.

Thank you.

9
  • What do you mean by "in a good way"? Commented Apr 20, 2016 at 14:27
  • 1
    @John why would you give someone such an advice? go use frameworks build by others so you learn absolutely nothing other than to program with someone else's work? nonsens .. Commented Apr 20, 2016 at 15:46
  • 1
    @dbf, Why don't you create your own operating system? Commented Apr 20, 2016 at 16:09
  • 1
    @dbf and of course you're using it Commented Apr 20, 2016 at 16:23
  • 1
    @John I asked you a question, why would you give someone such an advice.. But you can also continue adding useless comments .. Commented Apr 20, 2016 at 16:29

1 Answer 1

2

Check out http://php.net/manual/en/function.call-user-func-array.php

P.S. You do not have to use reflection in order to check if method on that object's instance exist. Single function call can be enough. Check out http://php.net/manual/en/function.is-callable.php

It would be nice if you would use more descriptive names. Now they are confusing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is what I need! Function call_user_func_array! Have a wonderful day!And thanks for second function :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.