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
}
Related
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
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..
Am working with wiredesignz modular extensions for codeigniter, and i was wondering if its possible to access a regular codeigniter controller's public property from a module's controller
for example, this is a regular ci controller
<?php
class Dog extends CI_Controller {
public $name;
function __construct()
{
$this->name = "xyz";
}
}
and this a module controller
<?php
class Test extends MX_Controller {
function get_name()
{
//access the $name property of the dog controller here
}
}
If you're using HMVC there's no reason why all or any of your controllers can't inherit from MX_Controller. You can have controllers in your normal application/controllers or application/core folders that inherit MX_Controller, they don't have to be "module" controllers.
If you need to access properties from one controller in another why not create a base controller e.g. MY_Controller that extends MX_Controller, put it in either application/controllers or application/core and then every time you create a "module" controller simply inherit from MY_Controller instead of MX_Controller.
Don't forget you can load any module controller and use it like a library class.
I want my controllers to be extended from my base controller (no from Zend_Controller_Action).
How can I extend my base Contoller from Zend_Contoller_Action. And where this custom base controller to be placed so it will be accessible to other contollers.
Thanks in advance
To extend write:
abstract class Mylib_YourBaseController extends Zend_Contoller_Action{
}
Create a directory called Mylib in the same place the Zend library is, that's it.
My_Controller_Action extends Zend_Controller_Action { ... }
Place it in library/My/Controller/Action.php , then in you're app.ini ( or at bootstrap ) you need to register the namespace My , then all you're controllers can extend My_Controller_Action .
Is it possible to call the member function of another controller in zend framework, if yes then how?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Here's another controller
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Please explain how i can access memberFunction() from second controller.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.
Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Note that this only works for action methods (“memberAction”), not arbitrary member functions!
If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.
You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.
Regards,
Rob...
I would suggest you to follow DRY and move those functions to common library place. For example create in library folder
My/Util/
and file
CommonFunctions.php
then call your class
My_Util_CommonFunctions
and define your methods
Now you can call them from any place in the code using your new namespace which you have to register.
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));
in any controller you can call your custom methods by using:
My_Util_CustomFunctions::yourCustomMethod($params);