Silex can’t find the routes

Asked

Viewed 118 times

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 that’s the problem:

Silex Error -

What am I doing wrong?

1 answer

1


What’s happening is that your route is / and you’re accessing /Silex/ in the snow.

You have at least three solutions to solve this problem.

Solution 1

Since you are using the Apache server, you can create a Virtualhost to point directly to your project directory.

For example, you create a Virtualhost with the name http://myApplication.place/ and it will point straight to the folder web of your project.

Virtual Host Documentation (in English): https://httpd.apache.org/docs/2.4/vhosts/

Solution 2

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

Example:

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

This way your project will be accessible at http://localhost:8000/

Documentation about the built-in PHP server: http://php.net/manual/en/features.commandline.webserver.php

Solution 3

This is the least recommended solution, but if so you can enter the path /Silex/ right on your route:

$app->get('/silex/', function() use ($app) {
    echo 'inside get';
   return $app->json(['Hello World!']);
});
  • Vlw man! I tested solution 2 and 3 to see if it worked, both correct. Solution 1 I didn’t even want to test, because I don’t want to keep creating a Virtualhost for every project I might create. I will use Solution 2, thank you!

  • I’m glad it worked out! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.