Routing annotation with Symfony4 doesn't match cause of DNS? - php

I'm trying to make the first page creating tutorial
https://symfony.com/doc/current/page_creation.html
So i use the annotation in my src/Controller/LuckyController.php
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('lucky.html.twig', array(
'number' => $number,
));
}
}
And when i try to go in this url
(i.e)OVHServer.fr/project/public/lucky/number
I got a 404 Not Found Error
I guess that it's because I didn't put my DNS in the the project/public directory but I want to know if you know a way for let the Url like that and get the road in my controller.
Thank's

DNS is not relevant. The routing takes effect after the configuration of the server.
The public directory of symfony 2 and 3 is named web, it's named public since symfony4
For example :
OVHServer.fr/app.php/lucky/number
OVHServer.fr/web/app.php/lucky/number
OVHServer.fr/project/public/app.php/lucky/number
When your server is configured to hide the app.php it would give you
OVHServer.fr/lucky/number
OVHServer.fr/web/lucky/number
OVHServer.fr/project/public/lucky/number

Related

PHP Symfony Semantical Error

I created src/Controller file named as sampleController.php:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class sampleController extends Controller
{
/**
* #Route("/hello")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('sample/number.html.twig', array(
'number' => $number,
));
}
}
My twig file is this:
<h1>Number is: {{ number }}</h1>
And routes.yaml is this:
sample_asd:
path: /hello
controller: App\Controller\sampleController::number
I've installed annotations and --dev profiler. But when I navigate to http://localhost:8000/hello it gives:
HTTP 500 Internal Server Error
[Semantical Error] The annotation "#Route" in method App\Controller\sampleController::number() was never imported. Did you maybe forget to add a "use" statement for this annotation? in C:\xampp\htdocs\Projects\symfony_learning\config/routes../../src/Controller/ (which is being imported from "C:\xampp\htdocs\Projects\symfony_learning\config/routes/annotations.yaml"). Make sure annotations are installed and enabled.
I didn't understand what is the problem.
Thanks
You did forget to include annotation class. Try to add this.
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Update
For symfony 4 you should use this path.
use Symfony\Component\Routing\Annotation\Route;

Symfony routing 500 error

It is the first time will use symfony. The setup went ok but now I have a problem with de routing... I have a controller called "GenusController" and use "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;".
My controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\BrowserKit\Response;
class GenusController
{
/**
* #Route("/genus")
*/
public function showAction(){
return new Response("Text");
}
}
When I tried to go to visite the url (http://localhost/aqua_note/web/genus) I get an 500 Internal Server Error. Is there some one can help me with this problem?
I am following this tutorial https://knpuniversity.com/screencast/symfony3/first-page#play
use:
use Symfony\Component\HttpFoundation\Response;
instead of
use Symfony\Component\BrowserKit\Response;

Symfony4: No route found for "GET /lucky/number"

I am starting to play with symfony4. I've just created new application and create new LuckyController. It works with routes.yaml configured in this manner:
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::number
With the following controller:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
But I want to use annotations. So I decided to comment routes.yaml. Following documentation that explain how to create a route in symfony I've made this:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
Sometimes this problem occurs because of cache.you need to run php bin/console cache:clear.then it will work fine.
In Symfony4 you have to install annotations bundle.
Run this command composer require annotations
Then restart Your project.
I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL. So the documentation says:
http://localhost:8000/lucky/number will work exactly like before
That did not work. This however did:
http://localhost:8000/en/lucky/number
As suggested in #Jack70 answer.
https://127.0.0.1:8000/en/random/number
This should work.
If you run the command php bin/console debug:router it would show you the list of routes available as you can see in that list, you need to add the locale (language) as first argument.
app_annotationsample_number ANY ANY ANY /{_locale}/random/number
If you use annotations - you need check config/routes/anotations.yaml.
You need check path to your controller or add new path, for example:
controllers:
resource: ../../src/Controller/
type: annotation
Also, you'll get that error if the route name appears more than once.
I ran into that error while developing Angular app, so in order to debug I tried that same route in Postman, which I knew used to work fine, but this time it did not throwing the mentioned error. Luckily, I have just added one new controller, so finding the culprit was easy: the problem was that after copying methods from web controller to api controller I forgot to change route name in one of the methods.
So you might wanna be careful when copying :)

Creating the first controller in Symfony

I have just started to learn Symfony. The fact is that I do things and they work but I don't understand what I am doing and that must be the wrong way starting to learn something.
Here is a simple controller:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
/**
* #Route("/hello")
*/
public function helloAction() {
return $this->render('', array());
}
}
So in this case when I type
/**
* #Route("/hello")
*/
my editor automatically adds this line of code:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
What is that doing?
I have learned that namespaces must match the directory path but there is no way I could find that path in my Symfony project. So is it outside of my project? If so, this will be good to know in case I want to upload my project on the web server.
I would appreciate if someone could please explain this "mystery".
Your IDE just added the dependency to Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;, which is required to use the #Route annotation.
It is placed under the vendor/ directory, which of course you will need to upload along with your sources.

Symfony2 - Creating a URL

I've just started using Symfony. I want to echo the $bookid when I call a URL like book/5 , but I stuck somewhere.
Here's my DefaultController.php file
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller {
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction() {
return $this->render('default/index2.html.php');
}
}
file: /Myproject/app/Resources/views/default/index2.html.php
<?php
echo $id;
?>
When I call the book/6 , I get a blank page. What's missing? Do I need to change somewhere else, too?
You should declare that variable in your action and pass it to your view.
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction($id)
{
return $this->render('default/index2.html.php', array(
'id' => $id
));
}
}
Whenever you have a parameter defined in your URL, you also need to "declare" it in your action function, so symfony maps it. Then, if you want to use it in your view, you have to pass it along.
If you are just starting with Symfony, I strongly recommend reading the Symfony Book and Cookbook. They are full of examples and relatively easy to understand, even for a newbie.
Other than that, the answer from smottt is correct. You add {id} in your route definition and receive it as parameter in your controller action.
Symfony book: Routing

Categories