Welcome Guest, Not a member yet? Register   Sign In
Using routes.php with Phil Sturgeon's REST_Controller
#1

[eluser]alexanderm[/eluser]
I have a working REST controller called api.php that extends Phil's.

I don't want every line of controller logic for my whole API in my api.php. I want people to hit:

www.me.com/api/<some_resource>
and
www.me.com/api/<some_unrelated_resource>

without having all the CRUD/etc. for some_resource and some_unrelated_resource in the same file.

I would end up with a file that's like 10,000 lines long. Yuck. I want the CRUD for some_resource to be self contained in a file so that I can easily copy it to create CRUD for some_other_resource.

I'm trying to use routes.php, but routes don't seem to work with Phil's framework. I got routes working for non-REST_Controller extension classes, so I'm confident I'm using them correctly.

What should I do?
#2

[eluser]DJMOHNL[/eluser]
as i read it i think that you mean:
you want the: domain.com/api/function
to be used as: domain.com/api/A_Variable_here/Another_Variable_Here

what the _remap function will do is convert:

www.yourdomain.com/controller/index/variable (index can be function name if there is one that matches the in url)

to

www.yourdomain.com/controller/variable (it sents the 'variable' to the index function of the controller if there is no 'function' name equal variable name.)

Code:
// it sends the domain.com/[1st_segment]/[2nd_segment]/[3rd_segment]
    // to the 1st if the 2nd not exists otherwise it keeps it intact
    function _remap($method)
    {
        if (method_exists($this, $method))
        {
            $this->$method($this->uri->segment(3));
        }
        else
        {
            $this->index($method);
        }
    }
copy and paste this code above inside your controller (or all controllers would be easy)
- remember to paste this code as the last function in your controller, as it is the last action to be handled if needed only.

Then use your controller functions as:
Code:
// This is the index() function inside your controller
    function index($variable=null,$variable2='default')
    {
        // here you put your code for the API function, no-one will see the /index/ part  
        // of the url in the browserbar.

        $this->load-view('someview');
    }
in the index() function you have 2 example variables: '$variable' and '$variable2'
you can set them to null, blank '' or a default value. You must remember, always set a null at least! you cant use index($variable,$variable2) this would give some errors on some request types.


Hope it helps.




Theme © iAndrew 2016 - Forum software by © MyBB