FOSUserBundle override template - php

i'm very new to symfony and i'm stuck on an error.
I already searched for this over and over again but i didn't find any fix:
I installed the FOSUserBundle and i want to override layout.html.twig template to be my homepage of the website. I created a new bundle, made it a child of FOSUserBundle :
namespace Emag\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle {
public function getParent(){
return 'FOSUserBundle';
}
}
I made a new file in the src/Emag/UserBundle/Resources/views/layout.html.twig and a new controller
class HomeController extends Controller {
public function homeAction()
{
return $this->render('UserBundle\layout.html.twig');
}
}
but i get this error:
Unable to find template "UserBundle\layout.html.twig".
here's also my routing.yml file:
emag_magazine_homepage:
path: /emag
defaults: { _controller: UserBundle:Home:home }

You should use the correct namespace; EMAGUserBundle not UserBundle inside your routing file and in the homeAction to get your code well organised,
and then you have to change :
return $this->render('EmagUserBundle\layout.html.twig');
to
return $this->render('EmagUserBundle:layout.html.twig');

Related

Why is php symfony framework method not used (controller)?

Any ideas what can be wrong? PHP Storm says, that homepage is unused. On webside there is still symfony homepage so it doesnt work even on site.
routes.yaml
index:
path: /
controller: App\Controller\QuestionController::homepage
QuestionController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class QuestionController
{
public function homepage()
{
return new Response('What a bewitching controller we have conjured!');
}
}
Update:
/**
* #Route("/")
*/
also doesnt work work homepage()
in config/routes create file annotation.yaml
with such code:
controllers:
resource: ../../src/Controller/
type: annotation
that's all, enjoy :)

404 not found , Symfony routing path problem

i create a controller pageController.php :
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class pageController {
public function index()
{
return new Response('<html><body>hello...</body></html>');
}
public function contactAction()
{
return new Response('<html><body>contact...</body></html>');
}
}
and here is the routes.yml
index:
path: /
controller: App\Controller\pageController::index
contact:
path: /contact
controller: App\Controller\pageController::contactAction
the index works fine, but the contact doesn't work!
Note: when I changed the path of index from "/" to "/index", it doesn't work anymore, it shows 404 not found
I don't want to use annotations until i want to fix this
contact:
path: /contact
controller: App\Controller\pageController::contact
Symfony will look for your contactAction you don't need to mention it in your YML
Ps: you don't call a route by a route name /index wont work but you call it by the path /
I don't know which version of symfony you are using, but if it is symfony 2.x then you should name your index method as indexAction (exactly like you have in contact).
routes.yml:
index:
path: /
controller: App\Controller\pageController::indexAction
controller:
public function indexAction()
{
return new Response('<html><body>hello...</body></html>');
}
You should also make sure that your routes.yml are properly loaded.

What is the use of Main File in Symfony 2 Bundle?

What is the use of Main File in Symfony 2 Bundle?
Below is the Default Path of a file:
Project->src->BundleName->BundleName.php
For Ex:
Symfony_Project/src/AppBundle/AppBundle.php
The Content Of Above file is Always Blank:
<?php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
}
What is the use of this file in symfony?
Why We Can use this File?
Is it Mandatory or Not? /Can we Delete it?
Why it is empty?
This file can be used to override any other bundle (your application bundles / third party bundles) and its resources. You can set parent bundle for a given bundle. For example, you are having FosUserBundle included and you want to override some of its actions/layout files etc.. To accomplish this, create you bundle UserBundle.php. add FosUserBundle as its parent like as follows :
// src/UserBundle/UserBundle.php
namespace UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Override controller:
// src/UserBundle/Controller/RegistrationController.php
namespace UserBundle\Controller;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
{
public function registerAction()
{
$response = parent::registerAction();
// ... do custom stuff
return $response;
}
}
Then within your UserBundle directory structure, you can override controllers/layout files etc..
For more info please refer this link : https://symfony.com/doc/2.8/bundles/inheritance.html

More than one controller in one Bundle (Symfony)

Is it possible to do more than one controller in Symfony? And if yes, how can I do it? (Controller, routing etc)
Because now I have:
Directory "Controller" and inside "DefaultController.php" (It has 1000 lines of code like now.)
Directory Resources->Config->Routing.yml (Here I have all routings).
Directory Resources->views->Default (In this dir I have all views).
Is it possible to make more controllers? For example one controller will have only indexAction(), other will have addclientAction() etc.
So you will have controlelrs at src/MyApp/SomeBundle/Controller/
class OneController extends Controller
{
public function indexAction()
{
....
}
}
class TwoController extends Controller
{
public function addclientAction()
{
....
}
}
you routing.yml should looks like
my_route_index:
pattern: /
defaults: { _controller: MyAppSomeBundle:One:index }
my_route_addclient:
pattern: /addclient/
defaults: { _controller: MyAppSomeBundle:Two:addclient }
Yes, of course it is possible.
You can see an example here:

Setting $this->something in class body, can't use constructor dependency injection

I have a Symfony controller that uses a data_provider service. I can't figure out how to initialise this service.
I tried:
class DefaultController extends Controller {
public $dataProvider=$this->get('data_provider');
That causes an error, can't use constructor in a Controller so that leaves me with:
public $dataProvider=false;
public function someAction(){
$this->dataProvider=$this->get('data_provider');
...
public function anotherAction(){
$this->dataProvider=$this->get('data_provider');
...
So I have to set it every time in the controller action function. Is there an easy way to initialise the dataProvider when the controller is created?
The service is only for this bundle so it's defined in Symfony/src/mmt/myBundle/Resources/config/services.yml and that file is loaded by Symfony/src/mmt/myBundle/DependencyInjection/myExtension.php. Not sure if that makes a difference but I would prefer something that doesn't need changes to files outside the bundle.
Using symfony 2.3.4
[update]
After a seemingly endless list of instructions that cover less than half of what you need to do to get it working I got the injection part to work. Thanks to everyone giving me excellent advice.
My service is part of my bundle and don't want to change config files outside the bundle to load it. To make sure that Symfony/src/mmt/mrBundle/Resources/config/services.yml gets loaded you need a file called Symfony/src/mmt/mrBundle/DependencyInjection/mmtmrExtension.php (no, don't use just any name for the php file it's related to your application and bundle name).
What is in that file is explained here. I didn't need to do anything there because it was created when I created the bundle and have it create most of the files. (creating a bundle is in the standard documentation)
2.
Added a data_provider service in the services.yml file: (read standard documentation about setting up your db with doctrine)
data_provider:
class: mmt\mrBundle\Services\dataProvider
arguments: [ #doctrine.orm.entity_manager ]
Content of: Symfony/src/mmt/mrBundle/Services/dataProvider.php
<?php
namespace mmt\mrBundle\Services;
class dataProvider
{
protected $em;
public function __construct($em){
$this->em = $em;
}
public function getItem($id){
$item = $this->em->getRepository('mmtmrBundle:Item')
->find($id);
return $item;
}
public function saveItem($item){
$this->em->persist($item);
$this->em->flush();
}
}
?>
Now that I have the service I can use it in the controller like so:
$this->get("data_provider")->getItem(22);
But I would like my DefaultController have a $this->dataProvider when DefaultController is created. Preferably one depending on dev, prod, and test.
In comes dependency injection. Add the following to Symfony/src/mmt/mrBundle/Resources/config/services.yml
mmt.mr.DefaultController:
class: mmt\mrBundle\Controller\DefaultController
arguments: [#data_provider]
calls:
- [ "setContainer", [ #service_container ] ]
Now use the mmt.mr.DefaultController:indexAction (don't use mmtmrBundle:Default:index) in your routes:
/var/www/html/Symfony/src/mmt/mrBundle/Resources/config/routing.yml
mmtmr_homepage:
path: /{id}
requirements:
id: \d+
defaults: { _controller: mmt.mr.DefaultController:indexAction, id: false }
In Symfony/src/mmt/mrBundle/Controller/DefaultController.php should look like this:
<?php
namespace mmt\mrBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller {
public $dataProvider;
public function __construct($data_provider){
$this->dataProvider = $data_provider;
}
public function indexAction($id) {
$item=$this->dataProvider->getItem($id);
return $this->render('mmtmrBundle:Default:index.html.twig',
array('item' => $item));
}
}
?>
I think that's it, of something is missing please let me know. Congrats; you now know how to inject dependency (If you didn't already). The bad news is that by the time you read this it's probably out of date and you have to go to the Symfony site. Documentation is good there but didn't mention any of the things that broke it for me.
You should inject it in the controller using Depency Injection.
Based on the classes in your question you can do the following:
Symfony/src/mmt/mrBundle/Resources/config/services.yml
mmt.mr.DefaultController:
class: mmt\mrBundle\Controller\DefaultController
calls:
- [ setContainer, [ #service_container ] ]
- [ setDataProvider, [ #data_provider ] ]
Symfony/src/mmt/mrBundle/Controller/DefaultController.php
public function setDataProvider($provider){
if($this->dataProvider===false){
$this->dataProvider=$provider;
}
}
Make sure you use the service name and then the action in your router, for example:
Symfony/src/mmt/mrBundle/Resources/config/routing.yml
mmtmr_homepage:
path: /{id}
requirements:
id: \d+
defaults: { _controller: mmt.mr.DefaultController:indexAction, id: false }
mmt.mr.DefaultController is the name used in your services.yml and :indexAction is the function called indexAction in your DefaultController.php
I recommend to use a method that returns the service.
Something like:
public function getDataProvider()
{
return $this->get('data_provider');
}
And create a 'AdvancedController' that extends the Symfony2 Controller, put this method in it, and let all your controllers extend it.
In the AdvancedController you can put all your global methods that you use in controllers, it's really comfortable.
After use the depedency injection, $data_provider must be correctly given to your controller so why do you not use it in:
public function dataInit($data_provider){
$logger = $this->get('logger');
$logger->notice('from dataInit so this works');
}
maybe it's must be:
public function dataInit($data_provider){
$logger = $data_provider->getLogger();
$logger->notice('from dataInit so this works');
}
If no, please paste you dataProvider class

Categories