marcj / php-rest-service

Php-Rest-Service is a very simple and fast PHP class for server-side RESTful JSON APIs.
MIT License
216 stars 74 forks source link

404 Error on all route requests #27

Closed RipleWare closed 8 years ago

RipleWare commented 8 years ago

I have tried to follow your setup instructions the best I can but can't seem to get anything to work. Method 3 gives me compile errors when hitting the root directory:

Parse error: syntax error, unexpected 'throw' (T_THROW) in C:\wamp\www\Edge\MyRestApi\Admin.php on line 10

If I move to any route, I get 404 errors for every page or request I try. For example: http://webserver/edge/admin/page/1 returns The requested URL /edge/admin/page/1 was not found on this server.

Have I missed something?

marcj commented 8 years ago

Its throws a syntax error because of a typo: https://github.com/marcj/php-rest-service/commit/08c999907515c52583b365043d0428c864204cbf You shouldn't just copy&paste this code, since its a demo. Those functions like validLogin does not exist.

Please post your code that setups the php-rest-service class.

RipleWare commented 8 years ago

Thanks marcj for the reply. I was basing my setup from your sample, so my index.php look like this:

<?php

    $loader = include __DIR__ . '/vendor/autoload.php';
    $loader->add('MyRestApi', __DIR__.'/');

    use RestService\Server;

    Server::create('/admin', new MyRestApi\Admin) //base entry points `/admin`
        ->setDebugMode(true) //prints the debug trace, line number and file if a exception has been thrown.

        ->addGetRoute('login', 'doLogin') // => /admin/login
        ->addGetRoute('logout', 'doLogout') // => /admin/logout

        ->addGetRoute('page', 'getPages')
        ->addPutRoute('page', 'addPage')
        ->addGetRoute('page/([0-9]+)', 'getPage')
        ->addDeleteRoute('page/([0-9]+)', 'deletePage')
        ->addPostRoute('page/([0-9]+)', 'updatePage')

        ->addGetRoute('foo/bar/too', 'doFooBar')

        ->addSubController('tools', \RestApi\Tools) //adds a new sub entry point 'tools' => admin/tools
            ->addDeleteRoute('cache', 'clearCache')
            ->addGetRoute('rebuild-index', 'rebuildIndex')
        ->done()

    ->run();

?>

Then my MyRestApi/Admin.php file looks like this:

<?php

    namespace MyRestApi;

    class Admin {
        public function login($username, $password){

            if (!$this->validLogin($username, $password)
                throw new InvalidLoginException('Login is invalid or no access.');

            return $this->getToken();

        }

        public function logout(){

            if (!$this->hasSession()){
                throw new NoCurrentSessionException('There is no current session.');
            }

            return $this->killSession();

        }

        public function getPage($id){
            return 'Hello World';
        }
    }

    namespace RestAPI;

    class Tools {
        /**
        * Clears the cache of the app.
        *
        * @param boolean $withIndex If true, it clears the search index too.
        * @return boolean True if the cache has been cleared.
        */
        public function clearCache($withIndex = false){
            return true;
        }
    }

?>

Where I was calling the getPage($id) by routing to http://webserver/edge/admin/page/1. I was simply trying to get your example to return some dummy data before I introduced real API routing and proper functions, etc.

marcj commented 8 years ago

You should adjust the MyRestApi/Admin.php to something real, as this class won't work out of the box as its only a demo. At least remove there method calls to functions that do not exit, then it will work.

RipleWare commented 8 years ago

Just to let you know, I still couldn't get it to work even when using real functions. Gives me a 404 error as soon as I move away from the root directory. It like the routes are recognized.

Anyway, thanks for your help and your time tonight. Might check out some other examples.

Cheers

On Thu, 17 Mar 2016 10:05 pm Marc J. Schmidt notifications@github.com wrote:

Closed #27 https://github.com/marcj/php-rest-service/issues/27.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/marcj/php-rest-service/issues/27#event-593275515