CodeIgniter Forums
wrapper method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: wrapper method (/showthread.php?tid=71803)



wrapper method - blinkofaneye - 09-25-2018

Hi,

So the thing is i have a base class for all my controllers named MY_Controller.

PHP Code:
class MY_Controller extends CI_Controller
{
 
   ...
 
   protected function render()
 
   {
        echo 
"in render()\n";
 
   }
}

class 
Example extends MY_Controller
{
 
   public function method()
 
   {
        echo 
"in method()\n";
 
   }



what i want is CI calling render() after method() so http://www.example.com/example/method would produce this output:

in method
in render

my question is: "how can i do that?", calling render() in the destructor?


RE: wrapper method - ignitedcms - 09-26-2018

What is render()?


RE: wrapper method - dave friend - 09-26-2018

Super easy.

PHP Code:
public function method()
{
 
   echo "in method()\n";
 
   $this->render();


Or if you don't want method() calling render() directly

PHP Code:
class Example extends MY_Controller
{
    public function 
index()
    {
         
$this->method();
         
$this->render();   
    }

    public function 
method()
    {
        echo 
"in method()\n";
    }




RE: wrapper method - blinkofaneye - 09-26-2018

i think i didnt explain well Sleepy

PHP Code:
class Example extends MY_Controller
{
 
   public function method1()
 
   {
 
        // code example
 
        $this->set_title("method1");
 
        $this->add_script('script');
 
        $this->add_stylesheet('stylesheet');
 
        $this->add_content('view/example', array('args' => 1));
 
        // no call to $this->render() at the end, this should be done automatically 
 
   }

 
   public function method2()
 
   {
 
       // same
 
   }

 
   public function index()
 
   {
 
       // same
 
   }


the render() method actually calls $this->load->view() to add header, body and footer.

PHP Code:
class MY_Controller extends CI_Controller
{
 
   ...
 
   public function wrap()
 
   {
 
       $args func_get_args();
 
       $method array_shift($args);
 
       $this->{$method}(...$args);
 
       $this->render();
 
   }
}

// in config.php
$route[(:any)+/(:any)+(/:any)?] = '$1/wrap/$2$3'

i did this but it can lead to security issues


RE: wrapper method - InsiteFX - 09-27-2018

And how do think it is going to call it automatically if you do not invoke the method?