Wallogit.com
2017 © Pedro Peláez
Provide annotations for silex
Not recommanded for production, as it has no cache, and the project is unfinished (but working)., (*1)
Silex Simple Annotations allows you to get rid of controller providers and have it done your(our) way., (*2)
Prepend your controller function with annotations. We will deal with the rest., (*3)
/**
* @Path /login
* @Method POST
* @Bind user.login
*/
public function loginRegisterAction(Application $app, Request $request)
{
$user = SomeSuperStaticClass::loginOrRegisterThisGuy($request, $app);
return $app->json();
}
This examples links a POST on baseUrl/SomeControllerPrefixRouteYouGave/login to the loginRegisterAction action, (*4)
Currently, there seem to be only 2 providers after a quick Google search, (*5)
They both use Doctrine\Annotations to parse annotations and seem to work., (*6)
So why?
* You hate Doctrine (strange)
* You want a simple syntax
* You are lazy and just want to provide your /src for controllers
* You may need an automatic documentation. This will come in the future, (*7)
Why not? * These other providers are probably faster for parsing * I'm french, (*8)
First, add this to your composer.json dependencies, (*9)
"require": {
"other/packages...": "...",
"nicolascharpentier/silex-simple-annotations": "dev-master"
},
Then register the following service provider wherever you do so for your project:, (*10)
$app->register(new SilexSimpleAnnotations\AnnotationsServiceProvider(), array(
'simpleAnnots.controllersPath' => __DIR__ . '/../src/Controller',
'simpleAnnots.recursiv' => false, // Optional, default to false,
'simpleAnnots.controllersAsApplicationAwareServices' => false // Optional, default to false. Will instanciate controllers with $app as services
));
Parameters start with simpleAnnots and end with: * controllersPath : String or Array of controller directory/ies * recursiv : Boolean, if true the search for controllers will be .. recursiv. Not recommanded for optimization., (*11)
Loop the following for each controller you want to be annotation-equiped., (*12)
Give a prefix for the controller (which will be prepend for every action route), (*13)
/**
* @Prefix /user
*/
class UserController {
Pimp your actions, (*14)
/**
* @Path /logout
*/
public function logoutAction(Application $app)
{
AbstractUserManager::disconnectCurrentUser($app);
return $app->json();
}
/**
* @Path /stalk/{id}
* @Bind user.stalkhim
*/
public function stalkAction(Application $app, $id)
{
AbstractUserManager::disconnectCurrentUser($app);
return $app->json();
}
This will mean, (*15)
GET on /user/logout will call the logoutAction, which will be binded to user.logout
GET on /user/stalk/1 will call the stalkAction, with $id === '1' and the bind on user.stalkhim
Test and enjoy!!, (*16)
Controller Level, (*17)
Actions Level, (*18)
str_replace($function_name, 'Action', ''))Ps: Not working on this at the moment, still checking for issues., (*19)
Any sort of feedback is strongly encouraged and will be listened., (*20)
Also feel free to contribute or just ask for change, even the smallest, (*21)
"No more controller provider needed. Got my wife back. Love this" - George, Alabama., (*22)
"This is awesome, my legal e-commerce website got 200% more purchases" - Ben, Thailandia., (*23)