Dan Wears Prada
Dan Wears Prada

Reputation: 754

404 not found in 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

Upvotes: 12

Views: 7787

Answers (4)

Gladhon
Gladhon

Reputation: 261

I think you have configured your symfony correctly but something between your browser and symfony is broken. I would recommend to write a functional test for your config

https://symfony.com/doc/2.8/testing.html

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class LoginControllerTest extends WebTestCase
{
    public function testGetAction()
    {
        $client = static::createClient();
        $client->request('GET', '/api/test');
        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

If your the test work, everthing is okay with your config. Then you should take a look if you land on the right webserver and configure the webserver for symfony ( usually the root should be in the web folder )

Upvotes: 0

wonde
wonde

Reputation: 1181

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

Upvotes: 2

Etienne Noël
Etienne Noël

Reputation: 6176

Have you tried to run the following command?

php bin/console cache:clear --env=prod

Upvotes: 4

William Perron
William Perron

Reputation: 1398

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.

Upvotes: 4

Related Questions