We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

404 error with REST API

  1. I have following structure for my REST API:
        REST 
            \Client
                    \api
                            \shop
                                        .htaccess
            \Server
                        \api
                                \shop

                        \models
                        index.php
                        .htaccess

I do a proxying of all requests in the client with htaccess to Server.

When i send requests via GET like "https://localhost/REST/Client/api/shop/cars" i always get 404 error. (in my server .htaccess i point urls to index file like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /localhost/REST/Server/api/shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

here is my index.php file:

    <?php

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(array(
    __DIR__ . '/models/'
    ))->register();

    $app = new \Phalcon\Mvc\Micro();

    // Getting All cars

    $app->get('/cars', function() use ($app) {

     $phql = "SELECT * FROM Cars ORDER BY make";
    $cars = $app->modelsManager->executeQuery($phql);

     $data = array();
    foreach( $cars as $car){
        $data[] = array(
            'car_id' => $car->getCarId(),
            'make' => $car->getMake(),
            'model' => $car->getModel()
        );
    }

    echo json_encode($data);
    });

    $di = new \Phalcon\DI\FactoryDefault();

    // Db settings
    $di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "port" => 3307,
        "username" => "root",
        "password" => "",
        "dbname" => "carshop"
    ));
    });

    $app = new \Phalcon\Mvc\Micro($di);

    $app->handle();

Please advise!



14.9k
Accepted
answer

There was an htaccess error, Phalcon is good!!!