Welcome Guest, Not a member yet? Register   Sign In
Mini RESTFUL Controller functionality (like Laravel)
#1

[eluser]theshiftexchange[/eluser]
Hi all,

I recently checked out Laravel - and whilst I'm sticking with Codeigniter, I did like the way that Laravel allowed for RESTful controller routes.

So I thought I'd write something similar for me in Codeigniter. Note - this is not a RESTful server like Phil Sturgeon's code - this just allows some basic rerouting.

This means you no longer have to do:

Code:
function test()
{
   if ($this->input->post())
   {
       // do something with post data
   }
   else
   {
       // show something
   }
}

instead you can now do

Code:
function post_test()
{
   // do something
}

function get_test()
{
   // show something
}

function all_another()
{
   // You will see this whether you POST or GET to it - so you dont have
   // to duplicate all your functions - only the ones you want!
}

Note - the above works with "mysite.com/welcome/test" & "mysite.com/welcome/another" - it just looks to see if you "posted" something or not first!



All you need to do is insert the _remap() function below, and define the $restful variable in your controller. The best way to do all this is place it in MY_Controller.php - and that way you can turn "$restful" off/on in each controller as needed

<b>I've included some test functions</b> - so you can just copy + paste all the below into a "Welcome.php" controller that comes with the default CI install to test this in action. You obviously dont need all of them in your actually controller - all you need is _reroute() and $restful

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller
{

// Do you want "restful" functionality in this controller? Useful if doing
// this code in a MY_Controller - but dont want it on everywhere
protected $restful = true;

// You will only see this function if you "get" something - i.e. NO post!
public function get_test($msg = null)
{
  echo '&lt;form name="myWebForm" action="'.base_url().'welcome/test" method="post"&gt;';
  echo '&lt;input type="hidden" name="Test Value" value="Was Hidden!"&gt;';
  echo "<p>Generic test page</p>";
  echo '<p>'.$msg.'</p>';
  echo '<br>&lt;input type="submit" value="Submit some hidden value"&gt;&lt;br>';
  echo '&lt;/form&gt;';
}

// You will only see this function if you "post" something to "test"
public function post_test($msg = null)
{
  echo "you posted something!";
  var_dump ($this->input->post());

  // You could now do something like 'if $msg is correct, then do X, else redirect Y'
  // For this example - lets just show the get_test() function again
  $this->get_test();
}

// This function will show up no matter if you POST or GET to it
public function all_another()
{
  echo 'You got to "another" function!';
}

// If you turn "$resful" off in the controller - you will see this function
public function test()
{
  echo "test";
}

// Where all the magic happens
public function _remap($method, $params = array())
{
  // Firstly - set a default route back to just the request method
  // That way, if "reroute" is turned off - we have a default
  $reroute = $method;

  // Check if we want to actually use a restful reroute? Defined above in $resful = true;
  if ($this->restful)
  {
   // Default to the all_ method
   $reroute = "all_".$method;

   // Check if we "POSTed" something, and if so, if a "post_" function exists
   if (($_SERVER['REQUEST_METHOD'] == 'POST') && (method_exists($this, ("post_".$method))))
   {
    // A post function exists - so reroute to it
    $reroute = "post_".$method;
      }
      // If there was no POST or POST function, look for a "get_" function
      elseif (method_exists($this, ("get_".$method)))
   {
    // A get function exists - so reroute to it
    $reroute = "get_".$method;
      }
  }

  // This bit is the normal CI routing - it will check for the route, and go to it
  // This allows us to show the normal 404 error messages that you would expect to see
  if (method_exists($this, $reroute))
     {
         return call_user_func_array(array($this, $reroute), $params);
     }
     show_404();
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
#2

[eluser]Glazz[/eluser]
Hmm i'm trying laravel aswell and i also like how they implemented restful on controllers, one thing that i don't like is the performance, is way slower that CI, if i don't use the DB it works ok, but when i start using the DB it gets way slower..

I will try your method =)

Edit:

Instead of:
Code:
if ($this->restful)

You need to have:
Code:
if ( isset( $this->restful ) && $this->restful )

This way you don't get any errors if the $restful variable is not set.




Theme © iAndrew 2016 - Forum software by © MyBB