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
Related
I'm using the Fosuserbundle to manager members in my project { SF::3.4.8 },
when trying to override the controller of the registrationController by following the Symfony documentation
<?php
namespace TestUserBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOSUserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController {
public function registerAction(Request $request)
{
die("Hello");
}
}
but the system ignore that controller and still use The original controller, so if there any way to override my controller by
First, overriding the controller is probably not the best way to process. You should consider to hook into controller. Here is the related documentation: https://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html
Then if you still want to override the controller, you should act in the dependency injection. The service name of the controller is fos_user.registration.controller.
To replace the service you can simply use:
services:
fos_user.registration.controller:
class: YourController
arguments:
$eventDispatcher: '#event_dispatcher'
$formFactory: '#fos_user.registration.form.factory'
$userManager: '#fos_user.user_manager'
$tokenStorage: 'security.token_storage'
You can also override it in a CompilerPass. Which is probably the best solution for you because you do this inside another bundle.
Here is how it should look:
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ReplaceRegistrationController extends CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('fos_user.registration.controller')
->setClass(YourController::class)
;
}
}
Don't forget to register it inside your bundle:
$container->addCompilerPass(new ReplaceRegistrationController());
I created class named Task in the App\Entity namespace and my problem is in my another class I want to use it but it doesn't detect my class and give me error:
Attempted to load class "Task" from namespace "App\Entity".
Did you forget a "use" statement for another namespace?
this my Task.php:
namespace App\Entity;
class Task
{
protected $task;
public function getTask()
{
return $this->task;
}
public function setTask($task)
{
$this->task = $task;
}
}
This is my DefaultController that uses the Task class:
use App\Entity\Task;
class DefaultController
{
}
Depending upon your autoloader settings, if you are using composer, you can have it such that autoloading only happens from the pre-built class map - called 'authoritative classmap' (https://getcomposer.org/doc/articles/autoloader-optimization.md).
So, when you add a class to your source, it isn't recognised by the pre-built autoloader. Try running composer dump to let the autoloader load via standard PSR-0/PSR-4 rules.
If you are using the autoloader on PSR-0/PSR-4 mode, then maybe you've not put the file in the right directory.
In my app I have two diverse Bundles, BaseBundle and UserBundle.
When I'm in one of the controllers of UserBundle, how can I access the functions available in BaseBundle?
I'm in UserBundle and I'm trying to do something like:
$property['x'] = $this->calculateNumber(array($propertyX->indexX, $propertyY->indexY));
This is the error I get:
Attempted to call method "calculateNumber" on class "Example\UserBundle\Controller\DefaultController".
500 Internal Server Error - UndefinedMethodException
That's where Symfony's namespaces comes in handy. So when you are in UserBundle, just import the class containing the method you want to call:
# UserBundle/Controller/UserController.php
use BaseBundle\Controller\DefaultController;
//...
class UserController extends Controller
{
/**
* #Route("/whatever", name="whatever")
*/
public function whatever()
{
$base = new DefaultController(); //instantiate the class containing the desired method
$property['x'] = $base->calculateNumber(array($propertyX->indexX, $propertyY->indexY)); //call the calculateNumber method
}
}
You need to extend the BaseBundle with UserBundle and provide a public method called calculateNumber.
BaseBundle example:
<?php
namespace Example\BaseBundle;
class BaseBundle
{
public function calculateNumber()
{
// ...
}
}
UserBundle example:
<?php
namespace Example\UserBundle;
use Example\BaseBundle;
class UserBundle extends BaseBundle
{
// ...
}
As you can see, the UserBundle does not contain the requested function.
There are 2 ways of getting it there:
1) create UserBundle\Controller\DefaultController class by extending BaseBundle\Controller\DefaultController class. Then it will contain all parent's functions and properties
2) Create an actual object of BaseBundle\Controller\DefaultController with new() and use it to get the result.
Don't forget to add
use BaseBundle\Controller\DefaultController;
for both cases.
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');
I have followed some tutorials to create some global helper functions to use in blade views.
I have created ViewHelpers.php file in App\Helpers folder. This file contains the following code:
<?php
class ViewHelpers {
public static function bah()
{
echo 'blah';
}
}
Here is my service provider which loads my helpers (currently just one file):
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider {
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
echo $filename; // for debugging - yes, I see it is getting called
require_once($filename);
}
}
}
I have added it to config\app.php in 'providers' section:
'App\Providers\HelperServiceProvider',
And now I call my helper in a blade view:
{{ViewHelpers::bah()}}
For now it works fine.
But if I change my ViewHelper namespace to this:
<?php namespace App\Helpers;
class ViewHelpers {
// omitted for brevity
my views fail with Class 'ViewHelpers' not found.
How do I make my views to see the ViewHelpers class even if it is in a different namespace? Where do I add use App\Helpers?
Another related question - can I make an alias for ViewHelpers class to make it look like, let's say, VH:bah() in my views?
And I'd prefer to do it in simple way, if possible (without Facades and what not) because these are just static helpers without any need for class instance and IoC.
I'm using Laravel 5.
You will get Class 'ViewHelpers' not found because there is no ViewHelpers, there is App\Helpers\ViewHelpers and you need to specify namespace (even in view).
You can register alias in config/app.php which will allow you to use VH::something():
'aliases' => [
// in the end just add:
'VH' => 'App\Helpers\ViewHelpers'
],
If your namespace is correct you do not even have to use providers - class will be loaded by Laravel.