0

I have a project that uses the MVCish pattern.

I have a bootstrap file that takes info from GET to find out what controller to load up and the method from it but I'm stuck on the part on how can I pass additional GET parameters as parameters to the method being called.

My current bootstrap index.php file looks like this:

<?php
// sessions
session_start();

// config
require('common/Config.php');
// database
require('common/Db.php');
$_db = new Db();
// web
require('common/Web.php');

// main
require('cnt/Main.php');
$main = new Main($_db);

// current controller/view
$r = (isset($_GET['r']) && $_GET['r'] != '') ? $_GET['r'] : 'site/index';

// check auth
if($r != 'auth/index' && $r != 'auth/login'){
  if(isset($_SESSION['id'], $_SESSION['token']) && $_SESSION['id'] != null && $_SESSION['token'] != null){
    if(!$main->validateAuth($_SESSION['id'], $_SESSION['token'])){
      $main->redirect('auth/index');
    }
  } else {
    $main->redirect('auth/index');
  }
}

// render page
$ep = explode('/', $r); $cnt = ucfirst($ep[0]); $action = 'action'.ucfirst($ep[1]);

unset($_GET['r']);
if(!empty($_GET)){
  // additional parameters

}

include_once('cnt/'.$cnt.'.php');
$class = new $cnt($_db);
$class->$action();

How can I achieve this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Karl Viiburg
  • 832
  • 10
  • 34
  • This doesn't answer your question, but the way I would do it is not to determine the controller/view via a GET param, but by parsing the URL directly (like Zend or Symfony do, but simpler). Say you have `mysite.com/test/something`; parsing it, would result in the `TestController` and `somethingAction`. This way you won't have to `unset` certain "reserved" GET params. Take a look at http://php.net/manual/en/function.call-user-func.php for what you need. – Eduard Luca Oct 08 '14 at 07:30
  • You should abstract the user's input in a `Request` instance and pass it in `$class->$action($request);`. Maybe [**this**](http://stackoverflow.com/a/20299496/727208) helps a bit. – tereško Oct 08 '14 at 08:17

0 Answers0