symfony / flex

Composer plugin for Symfony
MIT License
4.16k stars 183 forks source link

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

Closed javiereguiluz closed 7 years ago

javiereguiluz commented 7 years ago

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: https://github.com/symfony/symfony/pull/21231

fabpot commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

see symfony/symfony#22383

stof commented 7 years ago

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

fabpot commented 7 years ago

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

fabpot commented 7 years ago

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.

stof commented 7 years ago

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

fabpot commented 7 years ago

@stof Done in symfony/recipes#99

darius-v commented 6 years ago

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 commented 6 years ago

@darius-v composer req annotations

darius-v commented 6 years ago

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

jkobus commented 6 years ago

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

weaverryan commented 6 years ago

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

AxelBriche commented 6 years ago

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 commented 6 years ago

Exactly the same issue than @AxelBriche here.

scandel commented 6 years ago

@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 commented 6 years ago

@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 commented 5 years ago

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 commented 4 years ago

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