At first sight I think you are not running on PHP 7. The operator ?? was introduce in PHP 7. If that's not the case, check the first line of your file, probably you have something like this "<"??php (had to wrap it with double quotes so Disqus can render it)
Unfortunately (fortunately?) I am, I had to update it from 5.6 to 7.1.3 to run Composer. Also, I think your "cheers" being in code format means there's something there that I'm not seeing?
Also thank you for being here to help, and being so responsive to everyone. Looking forward to being part of this community. :)
Hmm, your file looks correct. It makes think that you are not running on PHP7 in your web server (You may have installed PHP7 only to the CLI). Check the phpinfo of your webserver to double check what I'm saying. If that's the case, then you now know what to do :)
Hi everyone! If you have Symfony version 4.3 to use the annotations you need to insert this in your BlogController: use Symfony\Component\Routing\Annotation\Route;
They changed the directory, that's why so many of you are receiving error messages.
Tip: To easily configure a new controller you can use the terminal by writing php bin/console make:controller
I see you noticed this comment: https://symfonycasts.com/sc... - great, and I suppose your problem is solved. So basically, "Symfony\Component\Routing\Annotation\Route" class is a new official way to add routes annotations instead of deprecated "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" one. But instead of providing a BC break, Symfony warn us first saying it will be removed in future releases so you have time to update your code.
Nice to read you. Yeah i finally fixed my issue by replacing "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" with "Symfony\Component\Routing\Annotation\Route" class which is really cool.
Want to share my experience, SF 4.1.2 Deprecation Message: User Deprecated: The "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" annotation is deprecated since version 5.2. Use "Symfony\Component\Routing\Annotation\Route" instead.
The solution is pretty obvious, choose the other Route Class :)
Yep, that's totally ok :). When I recorded this tutorial, that package downloaded 6 things. But a few months ago, one of the packages was removed (nothing too important) and so now you only get 5.
I don't think that it's a good idea to use hierarchies like that on controllers. If you want to re-use some methods among controllers, then, you can create a base class to hold all that logic, or maybe use a treat, besides that, what a controller should only have are route actions
Hey MolloKhan I try to avoid code repetition. The simple option I have is to create 2 ( or more) actions in Controller, so actions make common stuff and some specific stuff for each. I thought about template design pattern.. Can you please be more detailed about the base class and the structure to organize it?
Thanks!
p.s. I thought about custom actions inside MyController, how is it?
public class MyController {
/** *@Route(/specific1, "specific1") **/ public function specific1() { $this->addItem(array $specificParams); }
public function addItem(array $specific1Params, string $specific1Function) { common(); $this->{$specific1Function}(specific1Params); }
Ohh, I see. Then, what you can do is to create a base abstract class that all your controllers with inherit and add all of your common logic/methods on it.
Tip: A controller should not *own* too much logic (and even less if it's business logic), so if you find yourself adding a lot of code to a controller that's a good hint that you may need a service class.
I'm sorry for the late reply. For some reason our software lost track of messages for a couple of days but it's back on business again!
As Vladimir said in the other comment. It seems like a problem with your web server. Probably this guide may help you out configuring it right https://symfony.com/doc/cur... But, I recommend using Symfony's built-in webserver, it's just easier and faster to use when working locally
Looks like you webserver doesn't pass you request to public/index.php. First page works because server pass / to index.php by default. If you are using apache behind the MAMP, than you should try to install symfony/apache-pack and it should fix it.
If anybody is having a 404 error from apache here's what i did to solve it: composer require symfony/apache-pack. manually create your .htaccess file and fill it with this: https://github.com/symfony/...
Thanks for sharing it :) If you want to know more about configuring Apache for a Symfony app you can read this guide: https://symfony.com/doc/current/setup/web_server_configuration.html You may have to add the DirectoryIndex index.php rule if your homepage never ends loading
How to validate url in the route. Can you please help me in that regards And can provide me the tutorial link on
Sure, you can do fancier things with routes, like match regular expressions, HTTP methods or host names - but that will all be pretty easy for you now.
We do cover some of it in further screencasts, but I'd not recommend you to jump to them directly and skip chapters to avoid losing context. Just go further in this track: https://symfonycasts.com/tr... . Otherwise, you can use our search for find related articles.
I am getting "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "workspace_show" as such route does not exist.").". When I run php bin/console debug:router, I can find the route name there. Where is than the problem coming from?
That's odd, it makes me think that you may have restricted that route to work with only specific request methods (such as POST). Let me see the output of your php bin/console debug:router BTW, try clearing the cache, I mean, just in case :)
Hi!! I have a question, when I want the autocomplete for my code php don't appear, but in my first test appear, I have the package php-autocomplete, but in this test don't work, what can I do? PD1: Sorry for my english. PD2: I'm using Atom instead phpstorm
If you're serious about getting auto-completion, use PHPStorm. It's that simple. You can get some really nice things from Atom (I use it as my editor for everything *except* developing), but PHPStorm is many levels better. You will get PHP autocompletion and Symfony-specific auto-completion. You can out our phpstorm tutorial to learn more about this: https://symfonycasts.com/sc...
Hello! When I use Route, i get error 404 Not Found.
ArticleController.php ` namespace App\Controller;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route;
class ArticleController {
/**
* @Route("/")
*/
public function homepage()
{
return new Response("My first page done! v2");
}
/**
* @Route("/news/why")
*/
public function show(){
return new Response("Future page to show one spse");
}
} `
Homepage is working. But "/news/why" page not working. I get error 404. Help to fix it, please.
First of all, try to clear the cache! Especially, if you're running your website in prod mode. Then, here's the trick to make sure if your route exist or no - debug it in console with "php bin/console debug:router" and search for "/news/why" in the output. Maybe your route has some prefix you forgot about :)
If nothing helped, please, double sure you use the correct namespace: "Symfony\Component\Routing\Annotation\Route". But it may depend on your Symfony version. For older version you probably need to use this one: "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" - at least you can try to use this one, but not for the latest version.
93 Comments
I'm at "omg my first page already" and I'm following along/started the project by myself, and I have this error loading index.php:
Parse error: syntax error, unexpected '?' in C:\www\eds-www\tuts\the_spacebar\public\index.php on line 15Here's line 15-17 of index.php:
`if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
}`
What have I fouled up?
Hey Nick S.
At first sight I think you are not running on PHP 7. The operator
??was introduce in PHP 7. If that's not the case, check the first line of your file, probably you have something like this"<"??php (had to wrap it with double quotes so Disqus can render it)Cheers!
Thanks for the reply MolloKhan!
Unfortunately (fortunately?) I am, I had to update it from 5.6 to 7.1.3 to run Composer. Also, I think your "cheers" being in code format means there's something there that I'm not seeing?
Also thank you for being here to help, and being so responsive to everyone. Looking forward to being part of this community. :)
> Also, I think your "cheers" being in code format means there's something there that I'm not seeing?
Yeah you are right! Disqus messed it up - I just fixed it
If you still have this problem. Could you show me your entire index.php file please?
> Also thank you for being here to help, and being so responsive to everyone. Looking forward to being part of this community. :)
You are welcom man :)
I can't get it to paste into code, so I'm linking a screenshot: https://imgur.com/a/07J6TBh
Does that help?
Thanks again!
Hmm, your file looks correct. It makes think that you are not running on PHP7 in your web server (You may have installed PHP7 only to the CLI). Check the phpinfo of your webserver to double check what I'm saying. If that's the case, then you now know what to do :)
Cheers!
Hi everyone! If you have Symfony version 4.3 to use the annotations you need to insert this in your BlogController:
use Symfony\Component\Routing\Annotation\Route;
They changed the directory, that's why so many of you are receiving error messages.
Tip: To easily configure a new controller you can use the terminal by writing
php bin/console make:controller
Hope it helped!
Hi guys , I am a new beginner on this course. And i already face some issues.
someone can help me with this please.
After adding
/**
* @Route("/")
*/
I have this path but !!!!!!!
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
the last word Route is crossing. And it says that Class Route is depreciated
thank you!
Hey Yao,
I see you noticed this comment: https://symfonycasts.com/sc... - great, and I suppose your problem is solved. So basically, "Symfony\Component\Routing\Annotation\Route" class is a new official way to add routes annotations instead of deprecated "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" one. But instead of providing a BC break, Symfony warn us first saying it will be removed in future releases so you have time to update your code.
Cheers!
Hi Victor,
Nice to read you. Yeah i finally fixed my issue by replacing "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" with "Symfony\Component\Routing\Annotation\Route" class which is really cool.
Thanks for your assistance.
Yay, glad you had got it working before even we replied you. And yeah, it was an easy fix ;)
Cheers!
Want to share my experience, SF 4.1.2 Deprecation Message:
User Deprecated: The "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" annotation is deprecated since version 5.2. Use "Symfony\Component\Routing\Annotation\Route" instead.
The solution is pretty obvious, choose the other Route Class :)
Hey Mike,
Thanks for sharing it with others! And yeah, this one is an easy win :)
Cheers!
How long do you use Symfony for developing apps ?
Hey Yao,
Hm, I personally started to get familiar with Symfony since 2.1, but mostly started developing apps since 2.3 IIRC :)
Cheers!
Is it normal that the debug pack just has 5 symfony plugins instead of 6 ? bc in the tutorial he said it comes with 6.
Hey yao!
Yep, that's totally ok :). When I recorded this tutorial, that package downloaded 6 things. But a few months ago, one of the packages was removed (nothing too important) and so now you only get 5.
Cheers!
So that means you are senior right now... Great job
Haha, probably so... thanks! ;)
Cheers!
Thanks Mike it works.
In 3:00 Chapter 3 I only have client and env. Why it is different?
Hey Kutay Deril
What you mean by only client and env? Did you download the course code or did you started the project by yourself?
Cheers!
Hello!
I have controller and 2 child classes,
pretty similar to Template design pattern.
public class MyController
{
/**
*
*@Route("/add-item", name="addItem")
*/
public function addItem(){
$this->common();
$this->specific();
}
public function common() { ...}
}
} //MyController
public class Child1 extends MyController
{
public function specific( ) {...};
} // Child1
public class Child2
{
public function specific() {...}
} // Child2
I want to build links from twig so child class Child1::addItem be called.
Is there some special annotation for that?
How can it be achieved?
Thanks!
Hey Avraham M.
I don't think that it's a good idea to use hierarchies like that on controllers. If you want to re-use some methods among controllers, then, you can create a base class to hold all that logic, or maybe use a treat, besides that, what a controller should only have are route actions
Cheers!
Hey MolloKhan
I try to avoid code repetition.
The simple option I have is to create 2 ( or more) actions in Controller,
so actions make common stuff and some specific stuff for each.
I thought about template design pattern..
Can you please be more detailed about the base class and the structure to organize it?
Thanks!
p.s.
I thought about custom actions inside MyController,
how is it?
public class MyController
{
/**
*@Route(/specific1, "specific1")
**/
public function specific1()
{
$this->addItem(array $specificParams);
}
public function addItem(array $specific1Params, string $specific1Function)
{
common();
$this->{$specific1Function}(specific1Params);
}
} // MyController
Ohh, I see. Then, what you can do is to create a base abstract class that all your controllers with inherit and add all of your common logic/methods on it.
Tip: A controller should not *own* too much logic (and even less if it's business logic), so if you find yourself adding a lot of code to a controller that's a good hint that you may need a service class.
Cheers!
Not sure where my last comment went - or how to get the "support" that is advertised in the subscription promotional...
In this video, things seems to work as advertised.. until we get to the second method
`
/**
*/
public function homepage()
{
}
/**
*/
public function show()
{
}
`
I'm using MAMP to serve up these pages.. I've got a virtual host pointing at http://spacebar
So
http://spacebar/
<b>Works</b>
http://spacebar/news/item
<b>Does not work (e.g., throws a 404 page not found</b>
<b>The full controller page looks like this</b>
`
<?php
namespace App\Controller;
use Symfony\Com``ponent\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArticleController
{
/**
*/
public function homepage()
{
}
/**
*/
public function show()
{
}
}
`
Hey Thomas W.
I'm sorry for the late reply. For some reason our software lost track of messages for a couple of days but it's back on business again!
As Vladimir said in the other comment. It seems like a problem with your web server. Probably this guide may help you out configuring it right https://symfony.com/doc/cur...
But, I recommend using Symfony's built-in webserver, it's just easier and faster to use when working locally
Cheers!
First annotated thing worked...
Second... getting page not found...
(I shorted the URL to /news for brevity)
` /**
*/
public function show() {
}
`
Hey Thomas W.
Looks like you webserver doesn't pass you request to
public/index.php. First page works because server pass/to index.php by default. If you are using apache behind the MAMP, than you should try to installsymfony/apache-packand it should fix it.Cheers!
If anybody is having a 404 error from apache here's what i did to solve it:
composer require symfony/apache-pack.
manually create your .htaccess file and fill it with this:
https://github.com/symfony/...
And it should work just fine.
Hey Saul Solé
Thanks for sharing it :)
If you want to know more about configuring Apache for a Symfony app you can read this guide: https://symfony.com/doc/current/setup/web_server_configuration.html
You may have to add the
DirectoryIndex index.phprule if your homepage never ends loadingCheers!
How to validate url in the route. Can you please help me in that regards And can provide me the tutorial link on
Thanks
Hey Sourodipta,
I'd recommend you to take a look at docs about this: https://symfony.com/doc/cur... . In particular:
> like match regular expressions
https://symfony.com/doc/cur...
> HTTP methods
https://symfony.com/doc/cur...
> host names
https://symfony.com/doc/cur...
We do cover some of it in further screencasts, but I'd not recommend you to jump to them directly and skip chapters to avoid losing context. Just go further in this track: https://symfonycasts.com/tr... . Otherwise, you can use our search for find related articles.
I hope this helps!
Cheers!
Hi there,
What's the font which is used in screenshots?
Best regards.
Hey Abelardo L. ,
What screenshots do you mean?
Cheers!
Those which are integrated into your explanations.
Hi Abelardo L. the little notes that roll up in our videos use the Open Sans font
Thanks, but I referred to the font used to code your examples in PHPStorm.
Hey Abelardo L.!
It's Menlo - size 19 px :).
Cheers!
Do you know the Firacode font?
Hmm, I don't! I just checked it out. It looks crazy! Really beautiful! I imagine it takes some getting used to!
I am getting "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "workspace_show" as such route does not exist.").". When I run php bin/console debug:router, I can find the route name there. Where is than the problem coming from?
Hey Teodora Moldovan
That's odd, it makes me think that you may have restricted that route to work with only specific request methods (such as POST). Let me see the output of your
php bin/console debug:routerBTW, try clearing the cache, I mean, just in case :)
Cheers!
Thank you! I tried clearing the cache before and it didn’t work. However, it worked after I closed and reopened PhpStorm.
Really? PHPStorm? that's even weirder! haha, but I'm glad to hear that your problem is fixed now
Cheers!
Hi!! I have a question, when I want the autocomplete for my code php don't appear, but in my first test appear, I have the package php-autocomplete, but in this test don't work, what can I do?
PD1: Sorry for my english.
PD2: I'm using Atom instead phpstorm
Hey Mau España!
If you're serious about getting auto-completion, use PHPStorm. It's that simple. You can get some really nice things from Atom (I use it as my editor for everything *except* developing), but PHPStorm is many levels better. You will get PHP autocompletion and Symfony-specific auto-completion. You can out our phpstorm tutorial to learn more about this: https://symfonycasts.com/sc...
Cheers!
Hello! When I use Route, i get error 404 Not Found.
ArticleController.php
`
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArticleController
{
}
`
Homepage is working. But "/news/why" page not working. I get error 404.
Help to fix it, please.
Hey Dmitrii,
First of all, try to clear the cache! Especially, if you're running your website in prod mode. Then, here's the trick to make sure if your route exist or no - debug it in console with "php bin/console debug:router" and search for "/news/why" in the output. Maybe your route has some prefix you forgot about :)
If nothing helped, please, double sure you use the correct namespace: "Symfony\Component\Routing\Annotation\Route". But it may depend on your Symfony version. For older version you probably need to use this one: "Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" - at least you can try to use this one, but not for the latest version.
Cheers!
"Houston: no signs of life"
Start the conversation!