08.
Show them a Genus, and the 404
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
21 Comments
After this cast my production part was broken. Any route I try returns me a 404 or 500 errors. But there was no errors in code and the reason was simple - don't forget to clear the prod cache guys :)
Btw custom 404 is easy - https://ibb.co/f4NyrQ :)
Hey mlavrik ,
You're right, the most common mistake is to forget to clear the prod cache. Customizing error pages is really straightforward, we just keep it for another screencast. We have a simple screencast about it here: https://knpuniversity.com/s...
P.S. Your octopus is really cool - love it! :)
Cheers!
Haha, LOVE the Octopus :D :D :D
Hi guys!
I have a big problem regarding a specific route. So I'll start by explaining my two db tables.
Categories table:
id | name
-------------
1 programming
2 design
Articles table:
id | category_id | title | content
----------------------------------------------------
1 1 symfony ...
2 2 bootstrap ...
3 1 php ...
And I need a route like this: /app_dev.php/category_name/article_title. Some examples:
- /programming/symfony
- /programming/php
- /design/bootstrap
And I have to following action method:
/**
* @Route("/{categoryName}/{articleTitle}", name="article_show")
* @Method("GET")
*/
public function showAction(Category $categoryName, Article $articleTitle)
{
// Here I need to somehow query my db in order to achieve anchors like the ones above
// {{ article.title }}
// Can you please give me some advice in order to achieve my goal? Thank you!
}
// The relevant Twig code:
{% for category in categories %}
{% for article in articles %}
{{ article.title }}
{% endfor %}
{% endfor %}
The routes I obtain are ok, but the problem is that there are rendered too many links.
I read the official Symfony doc article ( http://symfony.com/doc/curr... ), but honestly I can figure out much from it, and I don't know if this applies for my case.
Hey Dan,
It's pretty simple - use entity manager!
Here will be 2 queries to the DB, but if you want, you can do more complex things: create method in ArticleRepository where you do INNER JOIN with Category, so you can do the same with just a single query.
However, if you want - you can do that with ParamConverter of course: look closely to the "options":
BTW, in any case, the category's name and article's title columns should be unique, otherwise you'll get problems.
Thank you!
I've dug a little bit for this problem, and I guess my case is the same as using an OneToMany association. I mean, one category can have multiple articles.
In fact what I need is:
- I list some related articles as links with Article.title as innerHTML, and when I hover over the link (and when I click that particular link), I want to have the Category.name as part of my link. So if I have: <anchor>Symfony</anchor>, because this article belongs to "Programming" category, I want to be able to generate the link as:
<anchor href="/programming/symfony">Symfony</anchor>
I tried to solve this problem in the SQL tab of phpmyadmin, and I came up with this query:
SELECT articles.title AS title,categories.name AS name FROM articles,categories WHERE articles.category_id = categories.id
which does the job done. Now, you might be right, and the only thing I might need is an INNER JOIN on those two tables.
Hey there
How can we generate unique routes? Or in other words, when we have two genus with the same name like "Octopus", the link between the list.html and the show.html will always show the same genus.
I hope you understand my newbie-question;-)
Thanks!
Michael
Hey Michael,
To prevent this you have to make the name unique, i.e. add a "unique=true" option to the property annotation like:
After this, if you try to set a name which already taken - you'll get an exception. So before saving new Genus object - you will ensure that there's no such name in the database, i.e. send an extra query to the DB which will check it. Very often for this purpose devs uses another property, which names as slug (or alias). Take a look at the fist 2 chapters in https://knpuniversity.com/screencast/collections course, which explains how to use the StofDoctrineExtensionsBundle to implement the sluggable behavior in your project. This bundle will handle a lot of work for you.
Cheers!
Hey Victor
Thanks a lot for your great and fast explanations!!
In my database it will give a lot of same names. So I had a look at the screencasts and everything is clear by now.
I like symfony and this really great sreencasts here!!
Awesome! You're welcome ;)
Cheers!
Hey Victor
Can you explain me, why do you use the name for the route and not the ID in the tutorial? Are there some advantages?
By the way: your tutorials are suuuuuuper!!! I really like them!!!
Yo Michael,
Sure! Well, actually it's not so important, you can use whatever you want... but IDs looks pretty old. Look at modern websites - they all use slugs instead of IDs. The first advantage is just it's more readable. Let me show you an example: http://example.com/php-tuto... - I can guess what I can find following this link due to the slug in it. But I have no idea what is the link about if it looks like: http://example.com/128. So ID is something internal, which you probably would like to use in admin panel (because it's simpler), but not for your users. Also, slugs help you to promote your website for search engines, i.e. it's a part of SEO. That's why slugs FTW!
P.S. Thanks for your kind word! ;)
Cheers!
Thank you very much for the great explanations!
Why are we passing genus.name to the show action via the href and not some abstraction of the primary key? Or is all this optimised in the framework? I'm also wondering why we're querying again in the show action (on name and not just by pkey/abstraction of pkey at that) . Is it because the collection created for the list is disposed of following the list show action?
Hey Max,
We just want to make our URLs more readable, because IDs is something internal. Here's my expanded answer about it: https://knpuniversity.com/s...
If you will still have any questions - let me know!
Cheers!
Yes, I understand why not the pkey but name is a slower lookup - this is why I mentioned "abstraction" of the pkey (as well as the actual url being rewriteable so it wouldnt have to actually show the key). I thought maybe the framework would then map that abstraction to the pkey for efficiency. Possibly I'm overthinking it. So here we are querying a list and getting all genuses - we then click a link and fetch the same one as we already had - I'm guessing this is normal in PHP web interfaces as we want to dispose of the original list query as soon as possible - buffering it to avoid the "show" lookup would make no sense on the big scale of things.
Hey Richard ,
Ah, I see what you mean! Yeah, you're right, searching by name is slower, so you're probably want to create an index for names, or probably makes them unique which will be even better. However, more ofter developers create a separate field and name it "slug" or "alias", which is unique. Btw, StofDoctrineExtensionsBundle could helps with it a lot. So in real project it's fair point and you have to think about some kind of slug field, but for simplicity, we just use names in this screencast ;)
Cheers!
Here's a fun one for you!
Ah... no images in comments.
Hey Tony!
This isn't a bad one at all :). This is a common issue - usually due to some slight differences in how your web server is setup (and some shortcuts that I took to save us some time - which help cause the issue)! We talk about this in the first tutorial when we put this part together: https://knpuniversity.com/s...
Are you using the built-in PHP web server, or something else like Nginx or Apache? And what is the URL you are going to in the browser when you access the site?
Cheers!
Oh, no. Not in my local dev. Here on KNP was trying to "send you a clever 404 page." No worries. ^_^
"Houston: no signs of life"
Start the conversation!