Reputation: 1258
I work with symfony 4 for create page.
create ArticleController like this:
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ArticleController extends Controller
{
public function index()
{
/**
* @Route("/")
* @Method({"GET"})
*/
return new Response(
'<html><body>Hello !!!</body></html>'
);
}
}
I install annotation using composer and this is my composer.json
file:
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.3",
"symfony/console": "4.2.*",
"symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/twig-bundle": "4.2.*",
"symfony/yaml": "4.2.*"
},
"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-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.2.*"
}
}
}
And add this .htaccess
file into public folder:
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
Now, When I check with browser url:smfony.local
I see symfony welcome page. I check route using routes.yaml
file and my route work fine and true(show Hello ! ! ! message) But annotation route not work for me.
How do can I fix this problem?!
Upvotes: 2
Views: 94
Reputation: 651
I think you need to move the annotation before the route:
/**
* @Route("/", name="index")
*/
public function index()
{
return new Response(
'<html><body>Hello !!!</body></html>'
);
}
Also make sure you add annotations composer require annotations
Upvotes: 2