User Roles
…exception.
Inside, at the beginning, add if (!$this->isGranted('ROLE_CAPTAIN')).
Creating a role is as simple as that. You don't need to register it anywhere, you can just
start using it! The only requirement is, to be recognized as a role, it must…
The REST API Tutorial
…easy stuff. Nope, we’ll attack the ugliest
areas of an API, like custom methods and where each piece of documentation
should live and why.
The Project: Resources and Links¶
The project? Introducing Code Battles: a super-serious site where programmers
battle against projects. After…
Our First Test
…tests!
Mmmm... where do we put this new test? We can technically put our tests anywhere within our project.
But when we installed symfony/test-pack, Flex created a tests/ directory which,
no surprise, is the recommended place to put our tests.
Remember that, in…
Setting Relations in Foundry
…want a lot more. This is a job perfectly suited for our good
friend: Foundry. Remove the manual code, then anywhere, say:
StarshipPartFactory::createMany(100):
And try the fixtures:
symfony console doctrine:fixtures:load
Uh-oh!
starship_id cannot be null in starship_part.
This…
Many To Many with Foundry
…createMany(100).
Below, set droids to DroidFactory::randomRange(1, 5):
This will assign anywhere between 1 and 5 random droids to each Starship.
Maybe you noticed something: we're setting a droids property here, but in
Starship, we do not have a setDroids() method! Normally…
Container to the Rescue
…the only code we
need to touch outside of BattleManager is right here inside Container.
We don't need to go anywhere else - like battle.php - and change anything.
We just say $container->getBattleManager() and the Container class will
take care of all of the…
Assets: Custom CSS and JS
…each individual CRUD controller. So we can control assets on a
global level or for just one section.
Let's make our change globally so that we can change the color of the sidebar on
every page.
Anywhere inside of DashboardController, go back to the…
Attachments and Images
…you'll see
a terms-of-service.pdf file. Move this into assets/, though it could live anywhere.
In TripController::show(), we need to get the path to this file. Add a new
string $termsPath argument and with the #[Autowire] attribute and
%kernel.project_dir…
Reusing Queries in the Query Builder
…super-duper if we could reuse
that logic instead of repeating it over and over again. Let's do that!
Anywhere inside here, add a new private function called
addFortuneCookieJoinAndSelect(). This will accept a QueryBuilder object (make
sure you get the one from Doctrine\ORM…
Allow Admin Users to Edit any Treasure
…we can add a shortcut to Foundry. Open UserFactory. We're going to create something
called a "state" method. Anywhere inside, add a public function called, how about
withRoles() that has an array $roles argument and returns self, which will
make this more convenient when…
Builder in Symfony & with a Factory
…Then, pass that manually into the builder as $this->logger:
We're seeing some of the benefits of the factory pattern here. Since we've already
centralized the instantiation of CharacterBuilder, anywhere that needs a
CharacterBuilder, like GameApplication, doesn't need to change at all..…
Access Token Authenticator
…perfect system just for this! Spin over and open up
config/packages/security.yaml. Anywhere under your firewall add access_token:
This activates a listener that will watch every request to see if it has an
Authorization header. If it does, it will read that…
Ajax-Powered HTML Updates & a CSS Transition
…item element also has a removing class, change the
opacity to zero. Thanks to the transition, that change will happen gradually.
And where does this removing class come from? Good question! We are going
to add it.
Back in the controller, right at the beginning…
index.php to HttpKernel::handle()
…a try catch! That's not important yet. But later, we'll
see what happens when an exception is thrown from anywhere.
The real logic of HttpKernel lives in this handleRaw() method. Scroll down
a little to find it. Ah yes: handleRaw(). This is the…
Transport: Do Work Later (Async)
…ImagePost - from the logic that actually does that work. And...
it's a nice coding pattern: it's easy to test and if we need to add Ponka to an
image from anywhere else in our system, it will be super pleasant.
But this pattern…
More on Routing And Dependency Injection Parameters
…starting in Symfony 2.1, you can use a dependency injection parameter
anywhere in your routing.
First, let’s start with a normal route:
# app/config/routing.yml
# ...
parameter_test:
The goal is to move the /are/you/a/robot part out of our code…
Processing Streams by Hand for Fun & Profit
…then
the HTML is passed to the Turbo Stream system... and it works its magic. But in
theory, you could grab some Turbo Stream HTML from anywhere and tell the Stream
system to process it. And... it's kind of fun!
Head to the homepage…
Dispatching a Message inside a Handler?
…handled by our command bus: we have a DeleteImagePost command and
DeleteImagePostHandler. But inside config/packages/messenger.yaml, we're not
routing this class anywhere, which means it's being handled immediately.
Oh, and notice: we're still passing the entire entity object into the…
Back to the Basics (Symfony local Web Server)
…logs, especially for the production environments
and there's some kind of a problem I need to debug the problem, it's kind of
hard to figure out where the problem is and where you got your exception. So
here we make it very clear…
Toast Notifications
…right that disappear automatically with CSS transitions and can tie
my shoes for me.
On our CRUD controllers, I'm already setting a success flash message... but I'm
not rendering it anywhere. In the templates/ directory, create a new _flashes.html.twig.
To start…
x
517