News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Master Zend Framework:
Accessing ServiceManager Services in Controller Plugins
July 31, 2014 @ 09:43:49

Matthew Setter has posted another new tutorial to his Master Zend Framework site today showing you how to access ServiceManager services in controller plugins. Controller plugins are a Zend Framework feature that allows certain events to trigger the plugin code during the lifetime of the controller.

I've seen some questions on Google+ and StackOverflow of late, regarding how to get access to the Zend Framework 2 database adapter, along with other ServiceManager-defined services, in a custom controller plugin. This type of setup can come in handy for a number of situations. You may want to access services such as caching, logging or databases and want to provide a simple interface for doing so. People seem really interested in how to do it, but how to get access to services from the ServiceManager doesn't seem to be as clear as it could be. Gladly, there's not much involved in actually doing it.

He shows you how to create a plugin for an existing module, creating the two needed classes and adding a new function to configure it. He starts with the plugin factory that can be used to generate an instance of the plugin. Next is the plugin class itself that extends the abstract plugin and controller plugin classes. The required database adapter is injected into it via a constructor injection. Finally, in the Module.php configuration, he creates a "getControllerPluginConfig" method that registers the new plugin and points to its class. A screencast is also provided showing the active development of the code.

0 comments voice your opinion now!
servicemanager plugin controller tutorial access zendframework2

Link: http://www.masterzendframework.com/servicemanager/accessing-servicemanager-services-controller-plugins

Paul Jones:
Action-Domain-Responder, Content Negotiation, and Routers
July 18, 2014 @ 10:17:57

In his latest post Paul Jones comes back to his proposed application structure, the idea of Action-Domain-Responder, and answers some questions about where content negotiation happens and routing.

While talking about Action-Domain-Responder on the Crafting Code Tour, one of the common questions I got was: "Where does content negotiation happen?" My response was always: "Where does it happen in Model-View-Controller?" That opened up a discussion on how content negotiation is a tricky bit that can go in different places, depending on how you want the concerns separated, and is not a problem specific to ADR.

He goes on and tries to answer the question a bit better, pointing out that "it's a problem for everyone" isn't really good enough to take action on. He works through the different pieces of the ADR pattern, trying to reason out where the right fit is. He suggests a "first filter" on the Controller level, more specifically at the Router level. That's not to say that the Router needs to know about content handling, but it does need to know how to pass that information on.

0 comments voice your opinion now!
action domain responder content negotiation routing

Link: http://paul-m-jones.com/archives/6020

NetTuts.com:
How To Display Post Meta Data on a WordPress Post
July 11, 2014 @ 10:44:41

NetTuts.com has a a recent tutorial showing you how to show the metadata from a posting in WordPress right along with the other post data.

During the course of the series, one of the things that we did in order to help demonstrate the object-oriented principles as well as some of the features of the WordPress API was build a plugin. Specifically, we built a plugin that allowed us to view all of the post meta data associated with a given post within the WordPress dashboard. [...] Since that particular post was written, I've received a number of different questions one of which has been how do we take the data displayed in the dashboard - that is, the post meta data - and display it on the front end of the web site. In this article, we're going to take a look at extending the plugin such that we can display the data on a single post page.

To display the data, they actually extend the plugin they've already made. They start with some of the issues of this method (and the data itself) that you might run into during the development. They create a "public" directory to store the cached metadata in and a manager class to handle the functionality. The class loads the data and uses output buffering to capture the data. A public hook is defined to call the "display" action on each page load and the results are passed out to the view.

0 comments voice your opinion now!
wordpress metadata plugin extend tutorial action

Link: http://code.tutsplus.com/tutorials/how-to-display-post-meta-data-on-a-wordpress-post--cms-21658

Master Zend Framework:
Change Layout in Controllers and Actions in Zend Framework 2
June 27, 2014 @ 10:07:20

Matthew Setter has a new post to his Master Zend Framework site today showing you how to change layouts in controllers and actions for a Zend Framework v2 based application.

In Zend Framework 2, if you want to change the layout just for one action or for every action in a controller, how do you do it? How do you do it without overriding the layout for every action throughout the entire application? In today's post, based on an excerpt from Zend Framework 2 for Beginners, we see how to achieve both of these requirements.

He talks about the framework's use of the two-step view pattern and what the "template_map" definition usually looks like in a default ZF2 application. He shows three different ways to do the view switching from the controller or action:

  • Override the default layout in your module
  • Override the layout per/action
  • Override the layout per/controller

Each of these comes with a bit of code showing you how to make it work. They move from simplest to more complex, with the layout per controller being the most complex. It's not that it's difficult, it's just that there's more involved to make it work. You can either do it at the controller level or at the module level.

0 comments voice your opinion now!
tutorial zendframework2 controller action change ayout

Link: http://www.masterzendframework.com/views/change-layout-controllers-actions-zend-framework-2

Matthias Noback:
Symfony2 Framework independent controllers parts 2 & 3
June 19, 2014 @ 09:45:34

Matthias Noback has posted the next two parts of his "framework independent controllers" series (it started here) looking at avoiding annotations and tying up some loose ends.

From part two about annotations:

In the previous part of this series we decreased coupling of a Symfony controller to the Symfony2 framework by removing its dependency on the standard Controller class from the FrameworkBundle. Now we take a look at annotations. They were initially introduced for rapid development (no need to create/modify some configuration file, just solve the issues inline!) [...] This might not seem such a big problem at all, but the SensioFrameworkExtraBundle is a bundle, which means it only works in the context of a Symfony2 application. We don't want our controller to be coupled like this to the framework (at least, that is the point of this series!), so we need to remove the dependency.

He shows how to decouple this functionality through a proper routing configuration, fetching the needed data yourself for the request and generating the request object yourself. In part three he covers some of the comments already made about the series and how to take the final steps to abstracting out the controllers: removing bundle names from templates, removing the HttpFoundation dependency and letting go of "action methods".

0 comments voice your opinion now!
controller independent symfony series part2 part3

Link: http://php-and-symfony.matthiasnoback.nl/tags/controller/

Matthias Noback:
Symfony2 How to create framework independent controllers?
June 18, 2014 @ 09:15:16

Matthias Noback has a new post to his site today, the first part of a series, looking at making framework-independent controllers for use in a Symfony2 framework-based project.

The general belief is that controllers are the most tightly coupled classes in every application. Most of the time based on the request data, they fetch and/or store persistent data from/in some place, then turn the data into HTML, which serves as the response to the client who initially made the request. [...] In this post I demonstrate that this high level of coupling is definitely not necessary. I will show you how you can decrease coupling a lot by taking some simple steps. We will end with a controller that is reusable in different types of applications, e.g. a Silex application, or even a Drupal application.

In this first part he focuses on a few places where the common practices lead to some unnecessary coupling between the controller and framework:

  • Using the framework helper methods
  • Using dependency injection (manually injecting instead)
  • Making the controller a service instead

The next post in the series will look at the use of annotations and how to refactor them out of the controller to remove yet another coupling point.

0 comments voice your opinion now!
framework independent controller symfony2 tutorial refactor decouple

Link: http://php-and-symfony.matthiasnoback.nl/2014/06/how-to-create-framework-independent-controllers/

Matthew Weier O'Phinney:
Better Understanding Controllers Through Basic Patterns
June 10, 2014 @ 09:53:21

In his most recent post Matthew Weier O'Phinney shares his own spin on the Action-Domain-Responder pattern (from Paul Jones): how the controllers in the ARD setup could be explained as facades.

Paul M. Jones has started an interesting discussion rethinking the MVC pattern as applied to the web, which he has dubbed Action-Domain-Responder (ADR). If you haven't given it a read yet, click the link and do that; this page will still be sitting here waiting when you return. I agree with a ton of it - heck, I've contributed to it a fair bit via conversations with Paul. But there's been one thing nagging at me for a bit now, and I was finally able to put it into words recently. Controllers - Actions in ADR - can be explained as facades.

Matthew starts off by defining the Facade design pattern with a quote from the infamous "Gang of Four" book: simply put, a simplified interface to a complex system. He provides a basic example of a facade that wraps some common steps for inserting and logging data with this kind of simplified interface. He applies this to the ADR pattern's controllers, pointing out that it handles a few complex steps "behind the scenes" common to marshaling and managing the request.

For me, thinking of Controllers and Actions as Facades has an additional benefit: it describes rather complex architectural patterns in terms of basic design patterns. I find the more I can reduce the complexity of a definition, the more likely I will understand and use it correctly.
0 comments voice your opinion now!
controller designpattern adr action domain responder facade

Link: http://mwop.net/blog/2014-06-09-controllers-as-facades.html

Paul Jones:
Refactoring To Action-Domain-Responder
June 06, 2014 @ 10:06:15

Paul Jones has a new post to his site today with a more in-depth look at his proposed "Action-Domain-Responder" design pattern and how to refactor an application, based on some with with the Aura framework, to use it.

The v1 version of the Aura framework includes a controller to handle web assets. The idea for this controller was that an Aura package might have images, scripts, and stylesheets that need to be publicly available, but in development you don't necessarily want to copy them to a public document root every time you change them. [...] That v1 version is a mess. The Controller handles the response-building entirely, and there is no Model separation at all. Let's try refactoring it to an Action-Domain-Responder architecture and clean it up some for a v2 version.

Associated code it just linked to, but he does summarize the steps needed to make the transition: extract the domain logic, move responses to a separate class and rename the controller to an action. He also shows how making this separation makes testing easier and links to examples of tests for each. He finishes the post with two final notes about the refactor. One points out that this method isn't the only way to handle this architecture shift and that the action returns a responder, not a response object.

0 comments voice your opinion now!
action domain responder designpattern architecture refactor

Link: http://paul-m-jones.com/archives/6006

Stephan Hochdörfer:
Controller classes vs. Action classes
May 19, 2014 @ 10:52:31

In this new post to his site Stephan Hochdörfer covers some of his own thoughts about the recently proposed application structure from Paul Jones, the "Action-Domain-Response" pattern. In this post Stephan compares the typical controller classes with an action class.

First of all I do have the feeling that controller classes make it harder to structure your logic. I have seen a lot of "God Controllers" that do a shitload of stuff. Stuff that is not really related to each other. [...] action classes tend to be rather small, typically less than 100 loc for us. That also helps a lot when trying to understand what`s going on. I am aware that there are developers out there who are afraid when it comes to dealing with a lot of classes. [...] That`s another bonus point for action classes: It is easier to search for a class name than a method name in most IDEs.

He goes on to talk more about "God controller" classes, their dependencies and how that compares to action classes only taking in what they need. He touches on the reusability of action classes as opposed to controllers and how they come in handy for storing common logic.

0 comments voice your opinion now!
controller action class compare logic dependency

Link: http://blog.bitexpert.de/blog/controller-classes-vs.-action-classes

NetTuts.com:
Working With Controllers in Symfony 2
May 15, 2014 @ 10:44:34

The NetTuts.com site has posted a new screencast looking at using one particular part of a Symfony2-based application, the controllers. In this new addition to their series (there's more on routing and some of the basics) they introduce you to the basic handling of controllers and how to make them work.

Today we'll take a quick look at controllers from within Symfony 2. The easiest way to get started learning, is to just dive right in, see some code examples and I'll explain everything as we proceed. [...] I hope this video has show you how easy and fun controllers are to work with in Symfony 2. We can use the console to generate them and link everything up so that it all works seamlessly.

In the screencast, they start with the "WelcomeController" to show you the basic structure of the class and the action methods they hold. There's also an example of the commands you'll need to do the controller generation and what all is included in the result (controller, template, route, etc).

0 comments voice your opinion now!
symfony2 controller screencast tutorial introduction

Link: http://code.tutsplus.com/tutorials/working-with-controllers-in-symfony-2--cms-21111


Community Events





Don't see your event here?
Let us know!


introduction language release tips application laravel community conference interview series list code development version podcast deployment api zendserver framework developer

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework