How to create base class for all my symfony controllers - php

I want to create a base controller class for all my controllers in Symfony, I am very new to Symfony, so don't be angry with dumb question. I am asking this question because I can't do something like this
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AbstractController extends Controller
{
public function __construct()
{
//...... check access level
$user = $this->getUser(); //This is not working, I don't have access to the Controller(the base class) properties
}
}
Now one of my controllers
class UserController extends AbstractController
{
public deleteUserAction(Request $request)
{
var_dump($this);// this will dump an empty class that is not instance of Symfony\Bundle\FrameworkBundle\Controller\Controller
//.... delete user
}
}
What is the best way to do this? please...
EDIT....
What I really want to do is to check whether a user privilege level is enough to access a particular action(e.g. deleteUserAction()) in a particular controller(e.g. UserController), I have a class that attach privilege level to all actions in all controllers. The check will be very efficient if it happens in a parent controller (e.g. BaseController's constructor) which is executed before UserController->deleteUserAction() but in the base controller I don't have access to $this.
I have tried voter and ACL none help my situation.
Thanks in advance.

I think second one is the best way to create your own class and use common function in it.
If you want to add some common functions of controller then it is not the proper way to add it into the Symfony default controller, Instead you can create BaseController and extend your all the controller with BaseController and your BaseController should extends Controller.
By this way the default controller of the symfony stay untouched.

simply use service controller... is shared:
http://symfony.com/doc/current/controller/service.html

Related

Laravel Extend BaseController and Vendor controller

I'm using Jacopo Authentication package in one of my sites, I'm extending it to add some methods and do stuff after I call its methods, I'm extending it like this:
<?php
use Jacopo\Authentication\Controllers\UserController as JacopoUserController;
class UserController extends JacopoUserController{
...
}
Now, the problem is that I need to use some methods from my BaseController, whats the best way to be able to use BaseController here? Should I just instantiate it? Maybe move as much logic as I can to a Model or Helper and duplicate a little code? Is there anything like this?
class UserController extends JacopoUserController extends BaseController{
Thank you in advance.
You can extend only one class in PHP. However you could create trait and put there functionality from Basecontroller and to use this functionality simple add use Traitname; to class that needs that functionality

Using the same function on two different controllers

I have two controllers on my website, let's call them MovieController and ReviewController.
What I want to do is to use one of MovieController's functions inside ReviewController.
The only thing I could think of is to extend MovieController instead of CController. However, it's hard for me to believe it's the right solution...
You have at least two options
1) Move the function to the main Controller, under components/Controller.php
class Controller extends CController {
2) Have another class extend from the above-mentioned main controller, and put your shared Movie/Review function inside it. Have both MovieController and ReviewController extend from this intermediate controller instead. Perhaps call it SharedController:
class SharedController extends Controller {
class MovieController extends SharedController {
class ReviewController extends SharedController {
You can:
1) create Helper class
2) use Traits/Behaviors
etc..

Zend controller extends from two class

In my application I have got a main controller in which I have a few methods. One of them checks if the user is logged in and if not it redirects the user to login form etc.
class FA_Controller_Auth extends Zend_Controller_Action {
public function preDispatch() {
//chceck user is login
}
}
aand all controllers extend from this main class
IndexController extends FA_Controller_Auth{}
but now I am building multi payment gatwey from Zend_Payment_Controllerso Payment_GatewayController should extend from Zend_Payment_Controller. But I need to chack if the user is logged in or not and I am looking for somthing like multi extend
Payment_GatewayController extends Zend_Payment_Controller, FA_Controller_Auth
I know there is no way to use multi extend class in PHP but maybe there is a better way to do this, action helper or plugin to check authorisation ?
Regards.
Regards.
You can only extend one class. This is exactly why using a base controller class is considered bad practice, as it limits you somewhat. Instead you should move the logic for both checks into controller plugins instead.

How can I write a beforeAction() that applies to all controllers?

I want to register a script within the beforeAction method(Yii Framework). But I don't want to repeat that method in every single controller, so my question is how can i create a beforeAction() that all controllers will inherit?
Thx,
The default yiic generated yii webapp has a Controller class in project/protected/components/Controller.php , and all controllers in the app inherit from this Controller.
That class is the perfect place to add the beforeAction.
Edit: Incase you have not used yiic and don't have this default Controller class, it's fine to add a new class that extends from CController, and then have your controllers extend from this new class. You can keep all the common functionality for your controllers in this parent controller class.
You need to create BaseController.php inside components directory. Inside this file you will inherit your BaseController from CController. Write your beforeAction there. After this you will need to inherit all you controllers from BaseController.
1) Create Common parent Controller with extended with CController (example SomeController)
2) register script in the beforeAction() in this Controller (example SomeController)
3) extend this controller of your SiteController or Module Controller.
<?php
class SomeController extends CController
{
public function beforeAction()
{
// Your Register Script
}
}
class SiteController extends SomeController
{
// public function actionIndex
}

MVC Optimization question Framework Zend

Just a quick logical question.
I have 2 Zend Controllers namely Merchant and Account. Merchant Controller has an action called editAction. My question is whether it is possible to have same action in Account as well without duplicating the code. I managed to create a include file for the view but I like to have a best way to manage my code. I currently have an idea of having an Helper class and invoke that helper class to both these Controllers. But I know someone would have got better solution than this. Please help me.
Thank you
The simplest solution would be to extend Zend_Controller_Action into your own base class and put editAction() into that. For example, assuming you have model classes named 'Account' and 'Merchant':
abstract class My_Controller_Action extends Zend_Controller_Action
{
protected $_modelName;
public function editAction()
{
$model = new $this->_modelName();
// Do your editing here.
}
}
class AccountController extends My_Controller_Action
{
protected $_modelName = 'Account';
}
class MerchantController extends My_Controller_Action
{
protected $_modelName = 'Merchant';
}
Keep in mind that this design implies that the code in editAction() would work for both Accounts and Merchants.
I think the best course of action would be to put majority of the logic inside your Models and keep your controllers lean. Your models can extend your own class which would have common operations in it.
You could also write your own class, it doesn't have to be a helper, and use it in the controllers to save the entity:
$saver = new My_Editing_Class();
$saver->edit("account",$this->getRequest()->getPost()); // Editing "account" with the POST data.
You then do your magic inside "My_Editing_Class".

Categories