0
votes

this is my url http://localhost/mymvc/index.php?url=controller/method I want to catch 'method' by $_GET Super Global Variable and get access class of Controller method. I want to get all method dynamic way

Here is my index file code.

<?php
    include 'inc/header.php';
    include_once 'system/libs/MController.php';
    include_once 'system/libs/Main.php';

    $url = $_GET['url'];
    $url = rtrim($url, '/');
    $url = explode("/", $url);

    include 'app/controllers/'.$url[0].'.php';
    $ctlr = new $url[0]();
    $ctlr->$url[1](); //---->(1) here is problem. I can't access my method this way  



    include 'inc/footer.php';
?>

Here is my Controller.php file code

<?php
/**
* controller
*/
class Controller extends MController{

    public function __construct(){
        // parent::__construct();
    }

    public function method(){
        echo "Mohammad Arman from method";
    }
}
?>

But when I show this output. Showing below this error and notice

**

1)Notice: Array to string conversion in C:\Users\USER\Documents\wamp\www\mymvc\index.php on line 15

2)Notice: Undefined property: Controller::$Array in C:\Users\USER\Documents\wamp\www\mymvc\index.php on line 15

3)Fatal error: Uncaught Error: Function name must be a string in C:\Users\USER\Documents\wamp\www\mymvc\index.php:15 Stack trace: #0 {main} thrown in C:\Users\USER\Documents\wamp\www\mymvc\index.php on line 15**

1

1 Answers

0
votes

You need to make sure the name you are using as the function name is properly delimited,

$ctlr->$url[1]();

could be interpreted as $ctlr->$url and then [1], but what you really want is...

$ctlr->{$url[1]}();