Welcome Guest, Not a member yet? Register   Sign In
wrapper method
#1

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?
Lumos...
Reply
#2

What is render()?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

(This post was last modified: 09-26-2018, 06:15 AM by dave friend.)

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";
    }

Reply
#4

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
Lumos...
Reply
#5

And how do think it is going to call it automatically if you do not invoke the method?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB