Fnx29
Fnx29

Reputation: 13

Symfony 4 - How to fix '404 Not Found' for a web api REST where routes does exists?

I'm trying to implement a REST API with symfony 4. My problem is that I can't reach my routes for my method GET and POST. Everytime I try, I get a 404 error.

But when I try to get my routes on symfony, I have :

Name                            Method   Scheme   Host   Path

app_statistics_poststatistics   POST     ANY      ANY    /api/stat
app_statistics_getstatistics    GET      ANY      ANY    /api/stat/{statId}

So my routes does exist, but I can't reach them.

I'm working on a Windows Environment with WAMP to work in local.

I've already tried my api on 2 windows environnment and on one Linux environnment with LAMP. Always getting a 404 Not Found.

The author of the tutorial told me that my api was working on his computer, but I don't have more info about his environnment. So, I expect the problem to come from the configuration of my virtual host.

Here is my virtual host:

<VirtualHost *:80>
    ServerName statroche.fr
    ServerAlias www.statroche.fr
    DocumentRoot "D:/other/symfony/testRoche/public"
    <Directory "D:/other/symfony/testRoche/public">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

Important note: I don't have anything in my public folder as I don't want any page, just the API, do I still need to put 'public' in DocumentRoot and Directory ?.

I would like to reach my API and then for the GET method for example, return a json. Actually, I can reach the / of my project but can't reach other routes.

Upvotes: 0

Views: 3626

Answers (3)

Martin
Martin

Reputation: 363

Update for Symfony 6 and 7.
Since .htaccess is no longer installed you have to add a composer package.

composer require symfony/apache-pack

Also some notes for linux users. (Ubuntu in my case)
I have a folder with my personal projects, so I don't work on the default /var/www/html folder.

What I do is just to create a link.

ln -s ~/Work/myProject /var/www/html/myproject

After doing this you can navigate to http://localhost/myproject
Also if is not a production machine, and is only you developing like my case, is a good idea to add apache some privileges over those files by adding your user to the www-data group.

sudo usermod -a -G yourUser www-data

# restart Apache
sudo systemctl restart apache2

change yourUser with the user your are using to login

Upvotes: 0

balzacLeGeek
balzacLeGeek

Reputation: 815

Instead of

<Directory "D:/other/symfony/testRoche/public">

Point apache directory Path to the main folder of your Symfony project like this

<Directory "D:/other/symfony/testRoche">

Then, use .htaccess file (place it in D:/other/symfony/testRoche/.htaccess to make public/index.php as the main endpoint if you go to http://localhost:80

The full apache config

<VirtualHost *:80>
    ServerName statroche.fr
    ServerAlias www.statroche.fr
    DocumentRoot "D:/other/symfony/testRoche/public"
    <Directory "D:/other/symfony/testRoche/public">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

The .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Upvotes: 1

Nico Haase
Nico Haase

Reputation: 12092

Even if you don't want to build an "old-style website" with Symfony, you have to use index.php to bootstrap Symfony and to make the API accessible

Upvotes: 2

Related Questions