[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:
<?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 '<form name="myWebForm" action="'.base_url().'welcome/test" method="post">';
echo '<input type="hidden" name="Test Value" value="Was Hidden!">';
echo "<p>Generic test page</p>";
echo '<p>'.$msg.'</p>';
echo '<br><input type="submit" value="Submit some hidden value"><br>';
echo '</form>';
}
// 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 */