how to reuse symfony route - php

I want to reuse a route in symfony as I used in Slim framework.
for example: in Slim I could define groups and then I would put the actions inside the group:
$app->group("example",..{
$app->get("/staff"...);
$app->get("test");
$aap->group("books",..{
$app->get("/"..);
$app->delete("/{id}")
})
});
And to access to the action test the url would be like: "domain/example/test", and to access staff: "domain/example/staff";
and to access book would be like: "domain/example/book/";
can I do this in symfony without having to go to every controller and put it manually.

The symfony documentation provide an example on how defines a prefix for all action routes:
/**
* #Route("/example")
*/
class ExampleController extends Controller
{
/**
* #Route("/staff")
*/
public function staffAction()
{
}
/**
* #Route("/test")
*/
public function testAction()
{
}
}
/**
* #Route("/example/books")
*/
class BookController extends Controller
{
/**
* #Route("/")
*/
public function indexAction()
{
}
/**
* #Route("/{id}")
* #Method({"DELETE"})
*/
public function deleteAction($id)
{
}
}

Related

Laravel 5.5 facade not found when using alias

I created a facade.
<?php
namespace VimeoServer\Facades;
use Illuminate\Support\Facades\Facade;
use VimeoServer\App\Repositories\Contracts\VimeoServerRepository;
class Vimeo extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return VimeoServerRepository::class;
}
}
I registered it inside service provider.
/**
* Register the application services.
*
* #return void
*/
public function register()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/vimeo.php' => config_path('vimeo.php'),
]);
}
$this->app->singleton(VimeoServerRepository::class, function () {
/*
* Config.
*/
$config = config('vimeo.connection');
/*
* Repository.
*/
$repository = new ConcreteVimeoServerRepository($config);
return $repository;
});
}
I registered the service provider:
VimeoServer\App\Providers\VimeoServerServiceProvider::class
and the alias :
'Vimeo' => VimeoServer\Facades\Vimeo::class
I am trying to use it inside Video controller.
If I add use VimeoServer\Facades\Vimeo in the controller everything works as expected.
The problem appears when I try to use the \Vimeo alias, the facade class could not be found.
"message": "Class 'VimeoServer\\Facades\\Vimeo' not found"

How to access current placeholder value?

Symfony 3.4.
I have annotations for my controller:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller
and want to access current {prefix} value directly from controller's methods (which aren't routed actions). How to get it's value?
Finally: $this->get('request_stack')->getCurrentRequest()->get('prefix')
Symfony will pass you the variables automatically if you use them as function parameters, like this:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller {
/**
* #Route("/{_locale}/some/path", name="_some_route_name")
*/
public function actualAction($prefix, $_locale) { /* ... */ }
}
Alternatively you can use the whole request like this:
/**
*
* #Route("/{prefix}", requirements={"prefix":"daily_task|event"})
*/
class TaskController extends Controller {
/**
* #Route("/{_locale}/some/path", name="_some_route_name")
*/
public function actualAction(Request $request) {
$prefix = $request->get('prefix');
}
}

ServiceProvider binded class not found

Case (L5.4)
Currently trying to write an api wrapper using the package development Laravel offers.
I got a ServiceProvider which binds the model (Niki::class)
class NikiServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/config/niki.php' => config_path('niki.php'),
]);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('niki', function () {
return new Niki;
});
}
}
A Facade which registers the name of the component
class Facade extends \Illuminate\Support\Facades\Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
public static function getFacadeAccessor()
{
return 'niki';
}
}
And a model
class Niki extends Model
{
/**
* Config
*
* #var array
*/
public function __construct()
{
$this->config = config('niki')['api_key'];
}
public static function getHouses()
{
$response = $this->config;
return $response;
}
}
Above files are located in packages/prsc/niki/src and are being loaded using the psr-4 autoloading:
"psr-4": {
"App\\": "app/",
"PRSC\\Niki\\": "packages/prsc/niki/src/"
},
Error
So now my problem, the bind in the ServiceProvider returns a FatalError because of the file not being found.
FatalThrowableError in NikiServiceProvider.php line 37: Class
'PRSC\Niki' not found
I think it's just a namespace problem. I'm not sure I have all the clue about your namespaces, but here is something that should work (if I did not misunderstood):
Replace:
return new Niki;
By:
return new \PRSC\Niki\Niki;
If it does not work, please add your namespaces in each code snippet you pasted.

Best Practices on namespaces : on Symfony, difference of perfs when using (sub)namespaces?

I'm building an API and I would ask something about using namespaces on a Symfony2 controller.
Is there a real difference doing :
<?php
namespace My\APIBundle\Controller;
use FOS\RestBundle\Controller\Annotations\View,
FOS\RestBundle\Controller\Annotations\Post,
FOS\RestBundle\Controller\Annotations\Get;
use [...]
class MyRestController extends Controller {
[...]
/**
* #View()
* #Get()
*/
public function getAction(Thing $thing) {
return $thing;
}
/**
* #View()
* #Post()
*/
public function postAction() {
}
or doing
<?php
namespace My\APIBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use [...]
class MyRestController extends Controller {
[...]
/**
* #Rest\View()
* #Rest\Get()
*/
public function getAction(Thing $thing) {
return $thing;
}
/**
* #Rest\View()
* #Rest\Post()
*/
public function postAction() {
}
Would the alias load everything in the given namespace, losing perfs ?
Or will it load only annoted classes, unitary ?

Wrapping all methods of a controller in Recess

In recess, I have a controller
/**
* !RespondsWith Layouts
* !Prefix user/
*/
class UserController extends Controller
{
......
}
I want to wrap all methods of the UserController using Iwrapper. I know how to wrap method of a normal class using IWrapper. But in the case of the controller, i 'm not being able to do it because the UserController is not instantiated and its methods are called automatically by the recess controller.
You can use annotations to add a wrapper to the controller class. For instance, I have a controller "nas"
/**
* !RespondsWith Json,CSV
* !Prefix nas/
*/
class NasController extends Controller {
/**
* !Route GET
* !VerifyPermission Module: data, Permission: read, UnauthorizedAction: noEntry
*/
function index() {
}
}
The VerifyPermission annotation will add a wrapper in the expand method
Library::import('recess.lang.Annotation');
Library::import('cirrusWorks.wrappers.VerifyPermissionWrapper');
class VerifyPermissionAnnotation extends Annotation {
protected function expand($class, $reflection, $descriptor) {
$module = $this->module;
$permission = $this->permission;
$unauthorizedAction = $this->unauthorizedaction;
$descriptor->addWrapper('serve',new VerifyPermissionWrapper($module,$permission,$unauthorizedAction, $reflection->getName()));
/* ... */
return $descriptor;
}
}
Then you can create the VerifyPermissionWrapper and the standard methods will be wrapped around your class method (before(), after(), combine())
class VerifyPermissionWrapper implements IWrapper {
function __construct($module, $permission, $action, $method) {
$this->module = $module;
$this->permission = $permission;
$this->action = $action;
$this->method = $method;
}
function before($controller, &$args) {
error_log('Before {$this->action} on {$this->method}');
}
}

Categories