Event Hooks
…
And this means that we can use standard Symfony event subscribers to totally kick
EasyAdminBundle's butt!
Create a new Event directory... though, this could live anywhere. Then, how about,
EasyAdminSubscriber. Event subscribers always implement EventSubscriberInterface:
I'll go to the "Code"->"Generate" menu…
Configuring the Encoder in security.yml
…me how you want to encode the passwords. I can't
read your mind - I'm just a PHP framework.
Remember how I kept saying we would encrypt the passwords with bcrypt? Do you remember
actually configuring that anywhere? Nope! We need to do that.…
The Drupal Console & Route Cache
…in your terminal. Next, move
it into a global bin directory so that you can simply type drupal from anywhere
in the terminal:
Hello Drupal Console! Now try drupal list:
This shows a huge list of all of the commands you can run. There is…
Keeping Problem types Consistent
…t want to mess up the title. Heck, I don't even
want to have to write the title anywhere - can't it be guessed based on the type?
I think so - let's just create a map from the type, to its associated title…
Making an Argument Available to All Controllers
…it’s so important, that we want to be able to
have an $isMac argument in any controller function anywhere in the
system. This won’t come from a routing wildcard like normal - we’ll figure
this out by reading the User-Agent.
As an…
Symfony Magic: Replace the _controller
…without borders
or boundaries. Let’s prove it.
We know that Symfony reads the _controller key in the request attributes
and executes that as the controller. This is set by the RouterListener
but it could be set anywhere.
Let’s go adventuring!. UserAgentSubscriber listens to…
An Aside: Dependency Injection Parameters
…server can have their own. So we set parameters
here and use them anywhere else.
Adding More Parameters¶
But technically, we can add parameters to any configuration file. Go back
to config.yml and add a new parameters key anywhere in the file. Below
it…
Restricting Edit Access to Owners
…we’ll just deny access. And remember, you can deny
access anywhere in your app just by throwing the special AccessDeniedException.
Since we’ll need the same security logic in editAction, updateAction
and deleteAction, let’s create a private function called enforceOwnerSecurity
that holds it…
Functional Testing
…app/ directory.
And hey! There’s a phpunit.xml.dist file there already for it to read. This
tells phpunit how to bootstrap and where to find our tests.
But we see a few errors. If you look closely, you’ll see that it’s…
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…
Custom Block View
…definition. Then we're going to map that to a template via block_views.
For step 1, open our netgen_layouts.yaml file and, really anywhere, add
block_definitions. This config can be used to create totally new blocks or
change options on existing blocks…
Contentful: Loading Data from an External CMS
…what we're doing, it's a pretty quick process and would give us a
lot of power on our site.
But one of the beautiful things about Layouts is that our value types can come from
anywhere: a Doctrine Entity, data on an external…
Centralizing Upload Logic
…code somewhere else:
in the new() action.
That's why I like to isolate my upload logic into a service class. In the Service/
directory - or really anywhere - create a new class: how about UploaderHelper?
This class will handle all things related to uploading files…
RAD with Symfony 3.3
…the goal: create an event listener that adds a header to every response.
Step 1: create an EventSubscriber directory - though this could live anywhere -
and a file inside called AddNiceHeaderEventSubscriber:
Event subscribers always look the same: they must implement EventSubscriberInterface:
I'll go to the…
Making all Services Private
…making this change is safe! We just created all of these service ids
a minute ago and they're not being used anywhere yet, definitely not with $container->get().
The exception is the last two services: we might be fetching these services directly
from the…
Adding a Search + the Request Object
…QueryBuilder for now.
Now for the magic. If we have a search, add an andWhere() that checks if
the lower case name of our Starship part is like our search. I know it looks
a bit funky, but that's because PostgreSQL is case-sensitive…
The Clever Criteria System
…this with a Criteria,
say addCriteria(self::createExpensiveCriteria()). Now that we're in a QueryBuilder,
we can do the normal stuff, like setMaxResults($limit). Want to do an orderBy
or an andWhere? Go for it. And of course, you can finish this with
getQuery()->getResult():…
The QueryBuilder
…That is exactly what we wrote before! So, no surprise,
if we remove that dd() and refresh... we're back to working! It's just that easy.
Okay, we have the QueryBuilder basics down. Let's get more complex by adding
andWhere() and orWhere() next.
Filtering / Searching
…set the old return value to a new $qb variable. Then, if ($filter) has some value,
add a where clause: andWhere('programmer.nickname LIKE :filter OR programmer.tagLine LIKE filter').
Then use setParameter('filter' , '%'.$filter.'%'). Finish things by returning $qb
at the bottom:
If you…
Custom Queries
…t need any arguments, and I'm going to simplify a bit. Let's
say andWhere('a.publishedAt IS NOT NULL'):
You can totally see how close this is to normal SQL. You can even put OR statements
inside the string, like a.publishedAt IS…
x
512