Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 not found in symfony

Tags:

php

symfony

I need a test function but it is not working. Supposedly with Routing + Controller should work, but I get a 404 not found.

This is my /app/config/routing.yml:

AndroidAPIBundle:
    resource: "@MDPIAndroidApiBundle/Controller/"
    type:     annotation
    prefix:   /

Inside Controller directory I have LoginController.php:

/**
 * @Route("/api/test", name="test")
 */
public function getAction(Request $request)
{
    $format = "json";
    $view = View::create()->setStatusCode(201)->setData(array("status"=>"OK", "data"=>array("test" => "DAN TEST")))->setFormat($format);
    return $this->get('fos_rest.view_handler')->handle($view);
}

So why when I make a curl request I get a 404 message? Also in browser adding /api/test to my local url.

EDIT

php app/console router:debug

test                                         ANY    ANY    ANY  /api/test

EDIT 2

/etc/nginx/sites-available/susy_android_final

server {
    listen                  80;
    server_name             susy.android.final.local;
    access_log              /var/log/nginx/susy_android_final.access.log;
    error_log               /var/log/nginx/susy_android_final.error.log;
    root                    /var/www/susy.mdpi.com.final/web;

    client_max_body_size 500M;
    large_client_header_buffers 8 64k; 

    location / {
            index app_dev.php;
            if (-f $request_filename) {
            break;
            }
            rewrite ^(.*)$ /app_dev.php last;
    }
    ## Parse all .php file in the /var/www directory
   location ~ (app|app_dev|app_eudev).php {
            fastcgi_split_path_info         ^(.+\.php)(.*)$;                
            fastcgi_pass                    unix:/var/run/php/php5.6-fpm.sock; 
            #fastcgi_pass                   127.0.0.1:9001; 
            #fastcgi_index                  index.php;
            fastcgi_param                   SCRIPT_FILENAME  $request_filename;
            include                         fastcgi_params;

   }


}

/etc/hosts

127.0.0.1 susy.android.final.local

This is in ubuntu

like image 508
Dan Wears Prada Avatar asked Sep 19 '17 14:09

Dan Wears Prada


3 Answers

Have you tried to run the following command?

php bin/console cache:clear --env=prod
like image 141
Etienne Noël Avatar answered Sep 19 '22 20:09

Etienne Noël


Make sure your domain is visible to Windows

The first step you should take to debug this issue is make sure Windows understands what www.yourdomain.com is and where it is located. To do this, you need to modify Windows's hosts file, which should be located in the C:\Windows\System32\drivers\etc\ folder. I personally like to create a shortcut for that file on my Desktop so it is easier to find whenever I need it. Open the file in Notepad++ (or whatever text editor you like), go to the end of the file, and add the following lines:

# replace with your VM's IP address
255.255.255.0 www.yourdomain.com

You can keep adding domains if you want to, just make sure you have a corresponding domain defined on your VM. Also make sure your VM has a static IP address, this guide is fairly complete.

Note, this step is only necessary if you are using a custom domain. If you use http://localhost:8080/, it shouldn't be a problem

Register the site in the Nginx config

If you created your project using Symfony's built in command, this step shouldn't be required. However, it doesn't hurt to check. Make sure the domain name correspond to the one in the hosts file described earlier, and that the root is defined correctly like so:

server {
    server_name yourdomain.tld www.yourdomain.tld;
    root /var/www/yourproject/web; # this is where app.php is located

You can find more on the minimum Nginx settings here.

Check your firewalls settings

So you've done all the steps above and it still doesn't work, now what? Maybe Windows is simply blocking the requests to the VM, in which case, you need to go in your Windows firewall and allow Virtual Host-Only settings.

I don't think this is where your problem comes from, as you clearly get an error message from nginx, meaning the request does indeed reach your server. However I think it may be interesting to know for other people.

like image 43
William Perron Avatar answered Sep 22 '22 20:09

William Perron


looks like you have .htaccess in your web directory with rewrite/redirect rules, since you moved the .htaccess rules to your virtual host configuration file delete the content of .htaccess file and try again

like image 32
wonde Avatar answered Sep 18 '22 20:09

wonde