Silex can not find routes

0

I have a Silex 2.0 application with PHP 7.0 and Apache 2.4 (on port 8080) with the following structure:

silex/
   | - vendor/
   | - web/
        | - index.php
   | - composer.json
   | - .htaccess

composer.json

{
    "require": {
        "php": ">=7",
        "silex/silex": "~2.0"
    }
}

.htaccess

FallbackResource /silex/web/index.php

web / index.php

<?php

define('APP_ROOT', dirname(__DIR__));
chdir(APP_ROOT);

use Silex\Application;

require 'vendor/autoload.php';

$app = new Application();
$app['debug'] = true;

echo "---------------- I am here! -----------------";

$app->get('/', function() use ($app) {
    echo 'inside get';
   return $app->json(['Hello World!']);
});

$app->run();

And the problem is this:

What am I doing wrong?

    
asked by anonymous 11.01.2017 / 22:02

1 answer

1

What is happening is that your route is / and you are accessing / silex / in the browser.

You have at least three solutions to solve this problem.

Solution 1

As you are using the Apache server, you can create a VirtualHost to point directly to the directory of your project.

For example, you create a VirtualHost named link and it will point directly to the web folder of the your project.

Virtual Host Documentation: link

Solution 2

Instead of using Apache, use the built-in PHP terminal server.

Example:

cd /home/pasta-do-seu-projeto
php -S localhost:8000 -t web/

That way your project will be accessible at link

PHP Embedded Server Documentation: link

Solution 3

This is the least recommended solution, but if so, you can enter the direct / silex / path in your route:

$app->get('/silex/', function() use ($app) {
    echo 'inside get';
   return $app->json(['Hello World!']);
});
    
11.01.2017 / 22:16