Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Can't load routing via annotations #23

Closed
javiereguiluz opened this issue Apr 3, 2017 · 19 comments
Closed

[Bug] Can't load routing via annotations #23

javiereguiluz opened this issue Apr 3, 2017 · 19 comments

Comments

@javiereguiluz
Copy link
Member

If I uncomment this in the default /etc/routing.yaml:

controllers:
    resource: ../src/Controller/
    type: annotation

And create a src/Controller/DefaultController.php with the appropriate annotations, I see this error:

error-routing-annotations


But this should work after this change: symfony/symfony#21231

@fabpot
Copy link
Member

fabpot commented Apr 5, 2017

The error message, while technically exact, is not the "right" one. By the ways, we have many of these issues now because we are using components individually. I've fixed some of them in 3.3, but I'm sure there are many other ones.

Here, the annotation loader is not registered as you probably don't have Doctrine annotations installed. Anyway, even when doing so, you also need SensioFrameworkExtraBundle to make it work.

Try composer req sensio/framework-extra-bundle annot and this should fix the issue.

To "fix" this, we should probably move the routing annotation loader definitions from the extra bundle to the core framework (possible as all classes are in the routing component). We can auto-register these loaders when annotations are available.

This would still need the Doctrine annotations lib, but adding a comment in the default routing.yaml would go a long way (which I will do now).

@fabpot fabpot removed the bug label Apr 5, 2017
@fabpot
Copy link
Member

fabpot commented Apr 5, 2017

There is another issue here: as we need to set minimum-dep to dev, it installs dev-master for all packages. But framework extra bundle at master is WIP and does not work with the any other dep as it's a major version. That's the kind of things that I think we cannot really fix and why Flex is really just a preview before Symfony 4.0.

@fabpot
Copy link
Member

fabpot commented Apr 11, 2017

see symfony/symfony#22383

@stof
Copy link
Member

stof commented Apr 12, 2017

@fabpot once Symfony 3.3 is stable, we won't need to set min-stability to dev though

@fabpot
Copy link
Member

fabpot commented Apr 12, 2017

Yes, of course, I was referring to people who will test Flex in the next few weeks.

@fabpot
Copy link
Member

fabpot commented Jun 14, 2017

Closing as we improved the error message and because 3.4 will support annotations out of the box without the need to install framework-extra bundle. We also added a note in the routing file.

@fabpot fabpot closed this as completed Jun 14, 2017
@stof
Copy link
Member

stof commented Jun 14, 2017

@fabpot we may want to create an annotations-pack requiring both doctrine/annotations and doctrine/cache though

@fabpot
Copy link
Member

fabpot commented Jun 14, 2017

@stof Done in symfony/recipes#99

@darius-v
Copy link

Should annotation routes work? I just installed symfony 4.

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

class PersonController extends Controller
{
    /**
     * Matches /blog exactly
     *
     * @Route("/", name="blog_list")
     */
    public function list()
    {
        // ...
        return 'b';
    }

    /**
     * Matches /blog/*
     *
     * @Route("/blog/{slug}", name="blog_show")
     */
    public function show($slug)
    {
        // $slug will equal the dynamic part of the URL
        // e.g. at /blog/yay-routing, then $slug='yay-routing'

        // ...
    }
}

This does not work. I think I do ok by documentation. Composer.json :

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "symfony/console": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/twig-bundle": "^4.0",
        "symfony/yaml": "^4.0"
    },
    "require-dev": {
        "symfony/dotenv": "^4.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-apcu": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "id": "01C1AMZG7RRHZQ87EJYRTRWDDA",
            "allow-contrib": false
        }
    }
}

I get error:

No route found for "GET /"

@chalasr
Copy link
Member

chalasr commented Dec 14, 2017

@darius-v composer req annotations

@darius-v
Copy link

@chalasr - so this maybe should be in documentation? At least I cannot see: https://symfony.com/doc/current/routing.html#creating-routes

@jkobus
Copy link

jkobus commented Dec 22, 2017

Couldn't get 3.4 to work with annotations as I had the same problem. Thanks @chalasr.

@weaverryan
Copy link
Member

I created a pr to the docs to add the install line.

@AxelBriche
Copy link

AxelBriche commented Mar 5, 2018

Hello, I have this error:

The requested URL /message was not found on this server.

composer req annotations

annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation

routes.yaml

#index:
#    path: /
#    controller: App\Controller\MessageController::index

MessageController.php

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MessageController extends Controller
{
    /**
     * @Route("/message", name="message")
     */
    public function index()
    {
        return $this->render('message/index.html.twig', [
            'controller_name' => 'MessageController',
        ]);
    }
}

php bin/console debug:router show the route:

Name Method Scheme Host Path
message ANY ANY ANY /message

But the route /message doesn't work.

Another routing problem:

"An error occurred while loading the web debug toolbar"
"The requested URL /_profiler/4029fd was not found on this server."

Note: It's a clean new Flex installation, I have followed the official doc.
Note2: I have run: composer require sensio/framework-extra-bundle, already installed.

@scandel
Copy link

scandel commented Mar 9, 2018

Exactly the same issue than @AxelBriche here.

@scandel
Copy link

scandel commented Mar 10, 2018

@AxelBriche, in my case I had forgotten the .htaccess, which is not created by default with symfony/flex. Posted a complete answer here: https://stackoverflow.com/a/49209861/2761700

@slootjes
Copy link
Contributor

slootjes commented Mar 10, 2018

@scandel in that case you would see an Apache "404 Not Found", not a Symfony error page so I don't think that can be it in this case.

edit: in case of @AxelBriche it does look like an Apache error indeed :)

@vincauddu29
Copy link

I have a same problem.

{ "type": "project", "license": "proprietary", "require": { "php": "^7.0.8", "ext-ctype": "*", "ext-iconv": "*", "sensio/framework-extra-bundle": "^5.2", "symfony/console": "4.2.*", "symfony/dotenv": "4.2.*", "symfony/flex": "^1.1", "symfony/framework-bundle": "4.2.*", "symfony/yaml": "3.4.*" }, "config": { "preferred-install": { "*": "dist" }, "sort-packages": true }, "autoload": { "psr-4": { "App\\": "src/" } }, "autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" } }, "replace": { "paragonie/random_compat": "2.*", "symfony/polyfill-ctype": "*", "symfony/polyfill-iconv": "*", "symfony/polyfill-php70": "*", "symfony/polyfill-php56": "*" }, "scripts": { "auto-scripts": { "cache:clear": "symfony-cmd", "assets:install %PUBLIC_DIR%": "symfony-cmd", "bin/docker-upgrade": "php-script" }, "post-install-cmd": [ "@auto-scripts" ], "post-update-cmd": [ "@auto-scripts" ] }, "conflict": { "symfony/symfony": "*" }, "extra": { "symfony": { "allow-contrib": "true", "require": "4.2.1" } }, "require-dev": { "ajardin/docker-symfony": "^0.4.0" } }

`<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class testController extends AbstractController {

/*
 * @Route("/api", name="index")
 */
public function index() {
    return new JsonResponse("test");
}

}`

/var/www/html # bin/console debug:router -vvv


Name Method Scheme Host Path


I am under nginx server and i have used nginx config :

`server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name symfony.localhost;
root /var/www/html/public;

ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

location / {
    # try to serve file directly, fallback to index.php
    try_files $uri /index.php$is_args$args;
}

location ~ ^/index\.php(/|$) {
    fastcgi_pass php:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;

    # When you are using symlinks to link the document root to the
    # current version of your application, you should pass the real
    # application path instead of the path to the symlink to PHP
    # FPM.
    # Otherwise, PHP's OPcache may not properly detect changes to
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
    # for more information).
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    # Prevents URIs that include the front controller. This will 404:
    # http://domain.tld/index.php/some-path
    # Remove the internal directive to allow URIs like this
    internal;
}

# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
    return 404;
}

}`
Can you help me ?

@achraf-jeday
Copy link

Folder names are capitalized make sure not to misspell them like I did in the routes and annotations definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests