Download the PHP package qafoolabs/no-framework-bundle without Composer
On this page you can find all versions of the php package qafoolabs/no-framework-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package no-framework-bundle
QafooLabs NoFrameworkBundle
Disclaimer: This is not an official Qafoo product but a prototype. We don't provide support on this repository.
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:
Disable view listener in SensioFrameworkExtraBundle if you are using that (not a requirement anymore):
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.
Returning TemplateView
Two use-cases sometimes occur where returning an array from the controller is not flexible enough:
- Rendering a template with a different action name.
- Adding headers to the Response object
For this case you can change the previous example to return a TemplateView
instance:
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.
In your controller you just return the view model:
It gets rendered as AcmeBundle:Hello:hello.html.twig
,
where the view model is available as the view
twig variable:
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
:
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:
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:
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:
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:
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.