Calling function in a controller from a parameter sent by the user in PHP

0

I'm building an application that, from the information the user sends me in a GET I'll execute a function. EX:

//classe
class Page extends CI_Controller {

    public class index($param){ 
      //aqui estou procurando algo semelhante a $this->"$param"();
    }

    private class foo(){ echo 'foo'; }
    private class bar(){ echo 'bar'; }

}

From the example above, if my user sent to the index('foo') function he would have to execute the $this->foo() function. Could someone show me how I can do my function index() execute another function depending on the parameter sent?

    
asked by anonymous 17.12.2015 / 03:10

1 answer

1

For you to do this is simple.

Let's create a controller named for example test and inside it do what you need:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teste extends CI_Controller {

    public function __construct(){

        parent::__construct();
    }

    public function ver($param){
        echo $this->$param();
    }
    private function foo(){
        echo 'foo';
    }
}

Access the URL and pass the command, as an example:

link

Will display

foo

If you want to create a controller to control every application and leave the URL organized, you can use Rotas , which is a good and easy way out:

link

    
17.12.2015 / 03:24