Chapters
33 Chapters
|
3:34:13
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
16.
Dynamic Roles
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.0
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.4
"symfony/cache": "^3.3|^4.0", // v4.1.4
"symfony/console": "^4.0", // v4.1.4
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.1.4
"symfony/lts": "^4@dev", // dev-master
"symfony/property-access": "^3.3|^4.0", // v4.1.4
"symfony/property-info": "^3.3|^4.0", // v4.1.4
"symfony/security-bundle": "^4.0", // v4.1.4
"symfony/serializer": "^3.3|^4.0", // v4.1.4
"symfony/twig-bundle": "^4.0", // v4.1.4
"symfony/web-server-bundle": "^4.0", // v4.1.4
"symfony/yaml": "^4.0", // v4.1.4
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.4
"symfony/dotenv": "^4.0", // v4.1.4
"symfony/maker-bundle": "^1.0", // v1.7.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.4
"symfony/stopwatch": "^3.3|^4.0", // v4.1.4
"symfony/var-dumper": "^3.3|^4.0", // v4.1.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.4
}
}
19 Comments
Just want to give a compliment. These tutorials are great. However, I have a general questions. The Model View Controller (MVC model) is a known way in the programming world. I am wondering why is Model called Entity in Symfony? Why is View called Template? Is this done on purpose, because there is an underlying difference? Or is it the same principle?
Hey Farry7,
Basically, it's the same principle. But those terms are coming from Doctrine that is standalone library, Symfony just has integration for it. And also because entity is a bit more complex actually as it's used for ORM. About templates, well, view is a concept, but actually template is something that implements that concept I think. Anyway, template is much more understandable for more people than view I think. And it also comes from Twig template engine, it is called so (template engine), so it makes sense it works with templates :)
P.S. Btw, Symfony officially is not an MVC framework but Request-Response framework ;)
I hope this helps :)
Cheers!
Hello Symfonycasts,
Instead of coding for a dropdown menu, I am wondering if you know there is a bundle for it out there? I have searched but could not find one?
Cheers!
Hey Dung,
Nope, unfortunately, I don't know any bundles for this. But actually we already use a library that implements this dropdown :) Twitter bootstrap is already handle a lot of useful code behind the scen that we can re-use. As developers, we just need to put proper HTML code with CSS classes to make it working and that's it. In case you want to make it even simplier - I'd recommend you to look into direction of Twig macros for example. you can add a few dropdowns, notice some code duplication and then refactor this code with Twig macros. Then just call those macros passing proper argument and macro will build the dropdown for you. But just avoid to overcomplecate things. Sometimes it's easier to just copy/paste some ready-to-use template from the Twitter Boostrap's docs page than overload your macros with many arguments. So, if you need one drowdown, creating a macro for this purpose is just an overkill as for me, easier to copy/paste the ready-to-use code from the docs :)
I hope this helps!
Cheers!
Hi victor
1) I actually ended up with just using standard Boostrap drop down html :) - they are clean and short
2) I appreciate you introduce Twig macros, I have seen it somewhere but never known what it is but now I do and I will learn it :)
Thanks for your insight.
Dung.
Hey Dung,
Yeah, probably the best solution! About macros, we have a Twig course, and we also talk about macros there as well, you may want to check it: https://symfonycasts.com/sc...
Glad it helped!
Cheers!
Thank you!
Hi Guys, I'm getting an error when trying to run this - PHP Fatal error: During class fetch: Uncaught ReflectionException: Class Symfony\Component\Form\FormTypeGuesserInterface not found in /vendor/symfony/doctrine-bridge/Form/DoctrineOrmTypeGuesser.php:25. Can anyone provide some help?
Hey Zesty
That's due a BC break between Symfony and latest PHP version. Here you can read more details about it: https://symfonycasts.com/sc...
Cheers!
How can I refresh user role after I add it?
For example I added new role to logged in user, and then redirect to page that requires this new role. For now i get access denied.
Hey unionelein
At the moment there isn't any built-in feature for it, but here is a PR where you can find some workarounds: https://github.com/symfony/...
Cheers!
How do i setup a form to give User ChoiceType option to choose his role
This doesn't seem to work. its still seen as a string, not array ( upon submit )
( i know that i should set multiple to true because it's an array ) but i want the user to be able to choose only one role )
`
->add('roles', ChoiceType::class, array(
))
`
Hey Remus M.
That's weird, you should see a list of radio buttons because you activated the 'expanded' option. Can you show me a screenshot of how the form looks like? Also, give it a check to the html generated by the Form component, probably there might be a hint of what's going on.
Cheers!
registration form
upon submit ->
Expected argument of type "array", "string" given at property path "roles".
Ohhh of course! That's because your `roles` property expects an array and since you are selecting only one option you get in a string. I believe you will need a DataTransformer for this task.
You can find more information here: https://symfony.com/doc/cur...
Or you can watch how Ryan implement one here: https://symfonycasts.com/sc...
Cheers & Merry Christmas to you too (a bit late)!
I've come up with this so far
`
->add('roles',ChoiceType::class, [
`
it seems to work :)
I knew that it expects an array :) ( its more than obvious )
I don't know how to transform that field ...
Hey Ad F.!
This is an AWESOME question :), and you already understand the problem well. So, here are some options, depending on what you need:
1) The solution you posted above uses "mapped => false". That WILL work, but you will then need to read that data manually in the controller to set the string value from the ChoiceType, turn it into an array, and set on the object - e.g.
This is not a fancy solution, but it should work.
2) More generally, we have "roles" set as an array in the database, because Symfony expects getRoles() to return an array. But if you have a simpler setup where you only ever assign one role, you could simplify this:
A) Change the
rolesproperty in User toroleand make it a stringB) Add getRole() and setRole() methods
C) In getRoles(), just return your one role, wrapped in an array (and maybe also with ROLE_USER)
D) In your form, the field would just be called "role"
Let me know if that makes sense!
Cheers!
weaverryan
I'm not sure if you are still looking for this but here's my solution to the same problem of wanting the User Roles displayed as a single dropdown menu...
As Ryan ( weaverryan ) suggested I added a Data Transformer to my FormType class
Building my Admin Roles dropdown menu
`
$builder->
->add('roles', ChoiceType::class, [
]);
`
And then the Data Transformer itself...
`
`
Worked a treat and no unmapping from the User entity needed.
"Houston: no signs of life"
Start the conversation!