• This repository has been archived on 28/Dec/2020
  • Stars
    star
    123
  • Rank 284,368 (Top 6 %)
  • Language
    PHP
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bundle to use Symfony controllers without depending on the Framework. Moved to GyroProject MVC-Bundle

QafooLabs NoFrameworkBundle

Disclaimer: This is not an official Qafoo product but a prototype. We don't provide support on this repository.

Build Status

This bundle is discontinued under this name and was moved to https://github.com/gyro-project/mvc-bundle

Goals

We want to achieve slim controllers that are registered as a service. The number of services required in any controller should be very small (2-4). We believe Context to controllers should be explicitly passed to avoid hiding it in services.

Ultimately this should make Controllers testable with lightweight unit- and integration tests. Elaborate seperation of Symfony from your business logic should become unnecessary by building controllers that don't depend on Symfony from the beginning (except maybe Request/Response classes).

For this reason the following features are provided by this bundle:

  • Returning View data from controllers
  • Returning RedirectRoute
  • Helper for Controllers as Service
  • Convert Exceptions from Domain/Library Types to Framework Types

Installation

Add bundle to your application kernel:

$bundles = array(
    // ...
    new QafooLabs\Bundle\NoFrameworkBundle\QafooLabsNoFrameworkBundle(),
);

Disable view listener in SensioFrameworkExtraBundle if you are using that (not a requirement anymore):

# app/config/config.yml
sensio_framework_extra:
    view:
        annotations: false

Returning View data from controllers

Returning Arrays

This bundle replaces the @Extra\Template() annotation support from the Sensio FrameworkExtraBundle, without requiring to add the annotation to the controller actions.

You can just return arrays from controllers and the template names will be inferred from Controller+Action-Method names.

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

class DefaultController
{
    public function helloAction($name = 'Fabien')
    {
        return array('name' => $name);
    }
}

Returning TemplateView

Two use-cases sometimes occur where returning an array from the controller is not flexible enough:

  1. Rendering a template with a different action name.
  2. Adding headers to the Response object

For this case you can change the previous example to return a TemplateView instance:

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

use QafooLabs\MVC\TemplateView;

class DefaultController
{
    public function helloAction($name = 'Fabien')
    {
        return new TemplateView(
            array('name' => $name),
            'hallo', // AcmeDemoBundle:Default:hallo.html.twig instead of hello.html.twig
            201,
            array('X-Foo' => 'Bar')
        );
    }
}

Note: Contrary to the render() method on the default Symfony base controller here the view parameters and the template name are exchanged. This is because everything except the view parameters are optional.

Returning ViewModels

Usually controllers quickly gather view related logic that is not properly extracted into a Twig extension, because of the insignficance of these data transforming methods. This is why on top of the returning array support you can also use view models and return them from your actions.

Each view model is a class that maps to exactly one template and can contain properties + methods that are available under the view template name in Twig using the same resolving mechanism as if you are returing arrays.

A view model can be any class as long as it does not extend the Symfony Response class.

<?php
# src/Acme/DemoBundle/View/Default/HelloView.php
namespace Acme\DemoBundle\View\Default;

class HelloView
{
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function getReversedName()
    {
        return strrev($this->name);
    }
}

In your controller you just return the view model:

<?php
# src/Acme/DemoBundle/Controller/HelloController.php

namespace Acme\DemoBundle\Controller;

class HelloController
{
    public function helloAction($name)
    {
        return new HelloView($name);
    }
}

It gets rendered as AcmeBundle:Hello:hello.html.twig, where the view model is available as the view twig variable:

Hello {{ view.name }} or {{ view.reversedName }}!

You can optionally extend from QafooLabs\MVC\ViewStruct. Every ViewStruct implementation has a constructor accepting and setting key-value pairs of properties that exist on the view model class.

Redirect Route

Redirecting in Symfony is much more likely to happen internally to a given route. The QafooLabs\MVC\RedirectRoute can be returned from your controller and a listener will turn it into a proper Symfony RedirectResponse:

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

use QafooLabs\MVC\RedirectRoute;

class DefaultController
{
    public function redirectAction()
    {
        return new RedirectRoute('hello', array(
            'name' => 'Fabien'
        ));
    }
}

If you want to set headers or different status code you can pass a Response as third argument, which will be used instead of creating a new one.

Add Cookies, Flash Messages, Cache Headers

when returning a View model, array or redirect route from a controller, without direct access to the response there is no easy way to add response headers. This is where PHP generators come in and you can yield additional response metadata:

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

use QafooLabs\MVC\Headers;
use QafooLabs\MVC\Flash;
use Symfony\Component\HttpFoundation\Cookie;

class DefaultController
{
    public function helloAction($name)
    {
        yield new Cookie('name', $name);
        yield new Headers(['X-Hello' => $name]);
        yield new Flash('warning', 'Hello ' . $name);

        return ['name' => $name];
    }
}

Inject TokenContext into actions

In Symfony access to security related information is available through the security.context service. This is bad from a design perspective, because it introduces a stateful service whenever access to security related information is needed.

To avoid access to the security state from a service, it needs to be passed as arguments, starting with the controller action.

That is what the TokenContext class is for. Just add a typehint for it to any action and NoFrameworkBundle will pass this object into your action. From it you have access to various security related methods:

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

use QafooLabs\MVC\TokenContext;

class DefaultController
{
    public function redirectAction(TokenContext $context)
    {
        if ($context->hasToken()) {
            $user = $context->getCurrentUser();
        } else if ($context->hasAnonymousToken()) {
            // do anon stuff
        }

        if ($context->isGranted('ROLE_ADMIN')) {
            // do admin stuff
            echo $context->getCurrentUserId();
            echo $context->getCurrentUsername();
        }
    }
}

For Symfony a concrete implementation SymfonyTokenContext is used for the interface that uses security.context internally.

In unit tests where you want to test the controller you can use the MockTokenContext instead. It doesnt work with complex isGranted() checks or the token, but if you only use the user object it allows very simple test setup.

Working with FormRequest

Handling forms in Symfony typically leads to complicated, untestable controller actions that are very tightly coupled to various Symfony services. To avoid having to deal with form.factory inside a controller we introduced a specialized request object that hides all this:

<?php
# src/Acme/DemoBundle/Controller/DefaultController.php
namespace Acme\DemoBundle\Controller;

use QafooLabs\MVC\FormRequest;
use QafooLabs\MVC\RedirectRoute;

class ProductController
{
    private $repository;

    public function __construct(ProductRepository $repository)
    {
        $this->repository;
    }

    public function editAction(FormRequest $formRequest, $id)
    {
        $product = $this->repository->find($id);

        if (!$formRequest->handle(new ProductEditType(), $product)) {
            return array('form' => $formRequest->createFormView(), 'entity' => $product);
        }

        $product = $formRequest->getValidData();

        $this->repository->save($product);

        return new RedirectRoute('Product.show', array('id' => $id));
    }
}

In tests you can use new QafooLabs\MVC\Form\InvalidFormRequest() and new QafooLabs\MVC\Form\ValidFormRequest($validData) to work with forms in tests for controllers.

ParamConverter for Session

You can pass the session as an argument to a controller:

public function indexAction(Session $session)
{
}

ParamConverter for Flash Messages

Passing QafooLabs\MVC\Flash is not supported anymore. You must migrate the code to use yield new Flash($type, $message); instead.


## Helper for Controllers as Service

We added a ``controller_utils`` service that offers the functionality
of the Symfony base controller plus some extras.

See my blog post [Extending Symfony2: Controller Utils](http://www.whitewashing.de/2013/06/27/extending_symfony2__controller_utilities.html)
for reasoning.

## Convert Exceptions

Usually the libraries you are using or your own code throw exceptions that can be turned
into HTTP errors other than the 500 server error. To prevent having to do this in the controller
over and over again you can configure to convert those exceptions in a listener:

    qafoo_labs_no_framework:
        convert_exceptions:
            Doctrine\ORM\EntityNotFoundException: Symfony\Component\HttpKernel\Exception\NotFoundHttpException
            Doctrine\ORM\ORMException: 500

Notable facts about the conversion:

- Both Target Exception classes or just a HTTP StatusCode can be specified
- Subclasses are checked for as well.
- If you don't define conversions the listener is not registered.
- If an exception is converted the original exception will specifically logged
  before conversion. That means when an exception occurs it will be logged
  twice.

## Turbolinks Support

To improve performance with traditional HTML response webapplications Basecamp
introduced [Turbolinks](https://github.com/turbolinks/turbolinks), a library
that uses Ajax to follow same domain links and then replaces only head title
and body to keep javascript and CSS in place.

The QafooLabsNoFrameworkBundle provides out of the box support for the
turbolinks JS library in the browser by setting the `Turbolinks-Location`
header after redirects.

More Repositories

1

php-refactoring-browser

A command line refactoring tool for PHP
PHP
558
star
2

php-profiler-extension

Deprecated, see Tideways/php-profiler-extension
C
27
star
3

gilded-rose-kata

Gilded Rose Kata for PHP
PHP
20
star
4

emscripten-opengl-example

A simple example of using emscripten to transpile an OpenGL rendered Cube from C to JavaScript
14
star
5

xhprof-collector

Deprecated in favour of QafooLabs/Profiler
PHP
10
star
6

TestingKatas

Base directory for testing katas
Puppet
9
star
7

profiler

Moved to tideways/profiler
PHP
9
star
8

Daemon

Library to implement background daemons in PHP
PHP
7
star
9

Lounge

Simple example CouchApp
JavaScript
3
star
10

testing-setup

Qafoo base repository for testing workshops.
PHP
3
star
11

TimePlanner

TimePlanner
PHP
3
star
12

bepado-demo-shop

Demo Shop to demonstrate usage of the Bepado SDK
PHP
3
star
13

Stub

CouchApp stub
JavaScript
3
star
14

profiler-packages

Package definitions for Qafoo Profiler
3
star
15

ansible-example

Simple example for using Ansible (with Vagrant)
HTML
2
star
16

Blog

CouchApp Demo App
JavaScript
1
star
17

redis-testing

Ruby
1
star
18

CouchDBExamples

Simple PHP CouchDB examples
PHP
1
star
19

symfony2-workshop

PHP
1
star
20

NodeJsCouchTesting

JavaScript
1
star
21

programming-language-catalog

A simple demo application using AngularJS. This Application does not have a certain purpose. It is supposed to only demonstrate the features of AngularJS
JavaScript
1
star
22

pouchdemo

A very simple application to demo the pouchdb replication feature
JavaScript
1
star
23

ErrorHandler

Helper library with common error handling code.
PHP
1
star
24

fixtures

Examples for the "Getting Fixtures Right" talk
PHP
1
star