Symfony2: Global configurable backend prefix for all bundles? - symfony

I'm looking for a global configuration for a routing prefix. There are a number of different bundles and I need a path to the backend controllers.
/**
* #Route("/admin/post")
*/
class PostAdminController extends Controller {
}
When I configure it that way, I have to edit a lot of files, if the prefix should be changed. Is it possible, to use a variable or a filter?
/**
* #Route("/%BACKEND_PREFIX%/post")
*/
class PostAdminController extends Controller {
}
Somewhere in configuration:
backend_prefix = admin

Ok, now I found a solution, but it seems working since version 2.1.
http://symfony.com/doc/current/cookbook/routing/service_container_parameters.html

Related

Can't use Route

Route("/) does not work.
I already tried to reinstall annotations and Symfony with Composer.
My routes.yaml :
index:
path: /
controller: App\Controller\ArticleController::homepage
My ArticleController.php :
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArticleController
{
/**
* #Route("/")
*/
public function homepage()
{
return new Response("TEST");
}
/**
* #Route("/news/test-test")
*/
public function show()
{
return new Response("FUTURE");
}
}
The page always shows the public/index.php, but it should show "FUTURE" ;)
in your config folder you should find a folder called routes inside it should be a file called annotations.yaml which contains the following :
controllers:
resource: ../../src/Controller/
type: annotation
if you have this you should get "TEST" when you go to ("/")
and you get "FUTURE" when you go to (/news/test-test)
and make sure to clear the cache if it did not
and you have to comment every thing inside routes.yml
Like Ali Mhanna said, check first annotations.yaml.
Then, according to Symfony documentation (read it, it can help ;), run php bin/console debug:router
If you see your route, your route is working.

Somfony 2 set header content mime-types automatically

I have a question about Symfony 2.
I would like to know how if there is a function implemented in Symfony 2 who return the content mime-type ?
Why mime-type cause trouble ? i have some file and i dont want everyone access to it then i made a methode who check if you have the right to access to this ressource.
chdir("Directory/".$nameoftheressource);
$file = file_get_contents($nameoftheressource);/**/
$namearray=explode(".", $nameoftheressource);
$extension=end($namearray);
$returnFile= new Response();
$returnFile->setContent($file);
if($extension == "css" )
{ $returnFile->headers->set('Content-Type', 'text/css');
return $returnFile;}
Thanks to you xabbuh this is near to work perfectly and as u said saved lot of time
now the code look like
EDIT
use Symfony\Component\HttpFoundation\BinaryFileResponse;
//some code
return new BinaryFileResponse('/Directory/'.$nameoftheressource);
But now it does display the css file but propose me to download it bt i would like to display it as a css normal css file
You can save a lot of code by using the BinaryFileResponse class which among other things automatically adds the right content type header.
It seems that you want to serve a protected CSS file. In this case, you can use the following code and protect the access to this controller using the Symfony Security system:
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
class DefaultController extends Controller
{
/**
* #Route("/css/app.css", name="css")
* #Security("has_role('ROLE_ADMIN')")
*/
public function renderCss()
{
$cssFilePath = $this->container->getParameter('kernel.root_dir').'/data/app.css';
$cssContent = file_get_contents($cssFilePath);
return Response($cssContent, 200, array('Content-Type' => 'text/css'));
}
}

Symfony 2/Sylius - Bundle loaded in AppKernel but can't load resource

I've generated a bundle (#ShopfishApiBundle) using generate:bundle like I have many times. It has automatically registered the bundle in theAppKernel and it added the loading of the bundle's routing.yml to the app/Resource/config/routing.yml as well. This is within a Sylius installation running Symfony 2.3
The #ShopfishApiBundle/Resource/config/routing.yml looks like this:
shopfish_api:
resource: "#ShopfishApiBundle/Controller/ProductController.php"
type: annotation
The product controller looks like this:
namespace Shopfish\Bundle\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/api")
*/
class ProductController extends Controller
{
/**
* #Route("/products")
* #Method({"GET"})
*
* #Rest\View
*/
public function allAction()
{
$products = array();
return array('products' => $products);
}
}
Loading any page instantly yields the below exception:
FileLoaderLoadException: Cannot load resource "#ShopfishApiBundle/Controller/". Make sure the "ShopfishApiBundle" bundle is correctly registered and loaded in the application kernel class.
In another Symfony2 (version 2.4) application I've made a similar bundle and this worked without error, I'm thinking something in Sylius messes this up. Do you know where I might solve this issue?
NOTE: I did a little test to see if a direct-no-annotations code snippet worked, and that seems to work. Though I want to use the FOS Rest bundle, using Annotations for Routing.
sf_api_controller:
pattern: /no-annotation-test
defaults:
_controller: ShopfishApiBundle:Product:all
I hadn't registered the essential SensionFrameworkExtraBundle in my AppKernel.php:
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle()
Thanks, #pazi!

Symfony 2.3 FOSUserBundle.en.yml not registering override

I am trying to override the default FOSUserBundle.en.yml from the FOS user-bundle.
I have the user bundle working fine, i have registered a user and logged in.
However when I copy the FOSUserBundle.en.yml into my own UserBundle to override the wording Symfony doesn't seem to pick it up.
This is the path I have copied the transation file to:
src/Blogger/UserBundle/Resources/translations/FOSUserBundle.en.ym
But no joy.. I have cleared the caches tried another browser but the change in the override will not come through.
I can place the change the in the original and see the changes:
vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/translations/FOSUserBundle.en.yml
Am I missing something?
Thanks,
John
To override the bundle, you should add this :
// src/Blogger/UserBundle/BloggerUserBundle.php
namespace Blogger\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Here's a link from the docs : http://symfony.com/doc/current/cookbook/bundles/inheritance.html
All the steps you should follow are in there.

How to add PunkAveFileUploaderBundle to sonata admin bundle

Well...Everything is in the title and here I am because the documentation about this topic doesn't exist really..So I hope some ninja developers will be able to give me some tips...
I'm working on my personal portfolio (Symfony 2.3) and I have a problem since a couple of week. I used sonata admin bundle to create my admin panel and have a lot of troubles to fix the different file upload in my admin. For that I was wondering to use the PunkAveFileUploaderBundle. But honestly I have no idea of how to implement it properly. I think I have to edit some sonata admin files but here again...which ones? I'm reading the files and have already a good idea of which ones to tweak but not sure at all.. By reading and following the documentation of sonata admin (sonata doc) I never had success.. Don't know why I'm actually following it step by step..
Well if anyone of you has an idea of how to implement the PunkAveFileUpload bundle with sonata admin bundle, let me know your tips or even better, a small example...
PS: links to the documentation are not needed, thank you.
May be you can read this post about getting Gedmo Uploadable working with Sonata Admin
I think you'll have to pass the punk_ave.file_uploader service in your admin class:
acme.admin.demo:
class: Acme\DemoBundle\Admin\DemoAdmin
arguments: [~, Acme\DemoBundle\Entity\Demo, SonataAdminBundle:CRUD, #punk_ave.file_uploader]
tags:
- {name: sonata.admin, manager_type: orm, group: demo, label: demo}
calls:
- [ setTranslationDomain, [SonataAdminBundle]]
ANd change your DemoAdmin class to manage uploads:
class DemoAdmin extends Admin
{
/**
* File uploader
*/
private $fileUploader = null;
/**
* Constructor
*/
public function __construct($code, $class, $baseControllerName, $fileUploader = null)
{
parent::__construct($code, $class, $baseControllerName);
$this->fileUploader = $fileUploader;
}
// ...
public function prePersist($object)
{
$this->manageUploads($object);
}
public function preUpdate($object)
{
$this->manageUploads($object);
}
/**
* Mannger uploads
* #param Demo $object
*/
private function manageUploads($object)
{
if ($object->getId()->getFile()) {
$this->fileUploader->syncFiles(...);
}
}
}
I really don't know if this will work, but that's the way I'll try to make it works...

Resources