Using the same function on two different controllers - php

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..

Related

How to create base class for all my symfony controllers

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

Use statement for child classes

I have a Controller class in which I have to use several namespaces like :
<?php
use Respect/Validation/Validator;
use Blah/blah/Foo;
class Controller {}
Now what I want that in every controller files that extends my Controller class, I do not have to write the use statements again and again.
This is something I want similar to what Laravel has done in his alias section.
How would I achieve this thing ? So for example when I do :
<?php
class HomeController extends Controller {
public function index()
{
$data = '';
Validator::arr($data); // Validator not found
}
}
This is unfortunately what you would have to do if using static methods. You could also have a Controller method (the constructor even) to inject the validator into the class, which the child classes can then use. You would the use public instance variables on the validator.
The benefit is that you do not need to specify the NS for your dependencies again and, especially so, your code is cleaner since you do not have a hard dependency on the Validator class.

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

Common functions in CodeIgniter

I am new to CodeIgniter.
I am using HMVC in CodeIgniter and want to use a module function in many other modules:
e.g I have a Locaton_model with function get_locations($param) { return; }
How do I use the above function in many other modules? Should I load the model in other module controllers every time I need this function or define the function some where globally?
You can easily achieve that by using core controllers:
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Instead of beginning your model with:
class Some_model extends CI_Model {}
You start with:
class Some_model extends MY_Model {}
Edit:
It is also possible to use libraries:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
This is useful when you want more general things, like a search engine, a IMAP interface, that kind of stuff.
if your creating your own model first make sure it is inside the core folder second on your controller extends the model name like this
class myController extends Locaton_model
{
function index()
{
$this->load->model->("your model name");
$this->yourmodelname->functionname($param);
}
}

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
}

Categories