Search code examples
laravel-4restler

Unable to get laravel/database and restler working with versioning


I have an existing website up and running, and now I want to add a REST interface to it in an api subdirectory. I'm not able to get this to work with versioning. I installed like so (no errors):

$ php ~/bin/composer.phar create-project laravel/database --prefer-dist api
$ cd api
$ php ~/bin/composer.phar require restler/framework 3.0.0-RC6

Then I uncommented the lines in public/index.php related to Restler and add a new API class that just echos a string. If I run this via php artisan serve and look at it through the localhost URL, then the method works.

Now I want to enable versioning, so I added these lines to public/index.php

use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;

And in app/controllers I created a v1 directory and moved Test.php into that. I also added a namespace directive to the file of the format namespace A\B\v1

When I restart the artisan server and query the API, I get a 404 error. I've tried as both http://localhost:8000/Test and http://localhost:8000/v1/Test

What have I forgotten to do?


Solution

  • Here is how I made it to work. Note the folder where I placed the api class file.

    in index.php

    use Luracast\Restler\Restler;
    use Luracast\Restler\Defaults;
    Defaults::$useUrlBasedVersioning = true;
    
    $r = new Restler();
    $r->addAPIClass('A\B\Test');
    

    Test.php kept in app/controllers/A/B/v1/Test.php

    <?php namespace A\B\v1;
    
    class Test
    {
        public function get()
        {
            return 'working';
        }
    }
    

    Both http://localhost:8000/v1/test and http://localhost:8000/test return "working"