I am currently developing a Lithium application and have come across a function that I have written that I would like to use across multiple Controllers.
I obviously don't want to have the function in each controller. What is the standard way of creating a re-usable component in Lithium?
The lack of search facility on their documentation is making it difficult to find any specifics.
You could try extending controller. Extending controllers is not that bad according to core devs. If that is not and option you could extract your code into a plugin, still some code in controller though.
All you have to do is create a extensions/action/Controller.php and have your controllers extend that.
In your extensions/action/Controller.php
<?php
namespace app\extensions\action;
class Controller extends \lithium\action\Controller {
protected function _init() {
parent::_init();
//add your functionality here
}
}
?>
And then, your controller has to extend the above mentioned base controller: class MyController extends \app\extensions\action\Controller {
I think this is not a Lithium-specific thing. You could either inherit from the Controller and make your own base controller, but you can also create arbitrary classes that hold your functionality. Don't let a framework inhibit you =)
Regarding the documentation: I usually google in the sense of "<keywords> site:lithify.me"
Related
To develop a project where I am creating some methods which are common in all the controllers.
Before this I have used codeigniter and there I wrote MY_Controller class in core directory and then I extended the controller in all the controllers in controller directory.
Same I want to do inside Laravel. But I am confused that where should I write the common methods like send_email, validate_captcha, ajax_file_upload and other common methods which remains same across whole application.
So please suggest me a good way to define such a class or middleware.. What should one do to create that?
OK. Let me suggest some stuffs
Need to write methods which is apply to all Controllers. You
can/should modify App\Http\Controllers\Controller.php. Because all
Controllers in Laravel extend it
Need to write class that available across whole application. It is
never easier
Step 1: Write any class you want in app folder. And follow psr-4 convention
Step 2: Register to Laravel application
In App\Providers\AppServiceProvider. In register() method. Add
$this->app->bind('bindname', function ($app) {
return new \App\YourClass;
// If you want to inject other class to YourClass contructor
// return new \App\YourClass($app->make('otherbindname'));
});
Step 3: Use it. There are several ways to access YourClass in your whole application:
app()->make('bindname');
app('bindname');
app()['bindname'];
\App::make('bindname');
//etc
I am using Cakephp 2.5.1. I have a common function which is to be shared by all controllers. I can either do it inside AppController.php or inside a Component. What is the difference? What are the pros and cons of the different methods? Under what conditions should I use which method?
when we write any function to the AppController.php the this function is available for all the Controller even if we don't need it,because all the Controllers extends the AppController.
Better to write function in component then we are able to use component in any specific Controller in which the function is required.
Its clearly mentioned in Documention :- link to documentation
I'm building a simple MVC framework in PHP and I'm stuck at the part where I have to create a BaseController class.
Every "page" controller needs to extend from this BaseController. Because this BaseController class will have properties that will give the user access to a template engine and a Logger class (and some other things).
The problem I have is that I'm not sure how to instantiate those things in the BaseController class. I can obviously hard code it in the __constructo() like this:
class BaseController
{
protected $view;
protected $log;
public function __construct()
{
$this->view = new \namespace\view('param1', 'param2');
$this->log = new Logger('name');
$this->log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
}
}
But this doesn't make it very modular. It makes it hard to change the Logger class for example. Or if the user wants to change the template engine to Smarty for example.
I also can't really use a form of Dependancy Injection. Because then every controller that extends from the BaseController will have to pass those instances to the BaseController.
That would look something like this:
class BaseController
{
protected $view;
protected $log;
public function __construct($view, $log)
{
$this->view = $view;
$this->log = $log;
}
}
HomeController
class HomeController extends BaseController
{
public function __construct($view, $log)
{
parent::__construct($view, $log);
// Do my own stuff
}
}
To me that's not really user friendly when a user only wants to do a simple thing in the __constructor. Not even with an IoC.
So the only thing that I can think of now is to use a Service Provider from the __construct method in the BaseController. But I'm not really sure if that's the way to go? Maybe there is a better alternative?
So what would be a good solution to solve this problem?
PS. And if I do need a Service Locator, are there any good examples out there? The ones I've found were written by people that seemed like they didn't really know what they were talking about. They were basically copying each others blog posts.
You first need to think what's a controllers responsibility, why would it need access to logging functionality or why would it be setting the template engine or switching templates?
The controllers job is simply to extract the data from the request(mostly form data/user input) and sending it to the model layer via a service, it should not be selecting templates, that's the views job.
You obviously want you keep the dependencies of your base controller to a minimum so that when instantiating child controllers you don't have a big list of dependencies to inject.
My base controller has two dependencies, the request object and a view which are both injected in. Controllers are simple and light, no logic or anything fancy should happen in them. Once ever I had to use a logger in one of my controllers but that was just because PayPal was sending a request to it using the IPN system and I needed to log all the data in the request to a file to see what was going on, other than special cases like that I can't see why a controller would need a logger. In this case only the controller which dealt with the PayPal request held an instance of a logger, not the parent base controller.
You should always inject your dependencies. Don't instantiate objects in constructors, it makes your class tightly coupled to those objects.
If you end up using a service locator it can be a sign that your classes break the single responsibility principle of OOP.
Controllers are meant to be simple, don't over complicate them by adding in dependencies you think they might need in the future, especially not to the base controller or you may have a lot of refactoring to do.
I am trying to learn how to use the Zend Framework and ive ran into trouble. Im trying to place the current users name in the header of the application (displayed on every page), specifically /layouts/scripts/default.phtml.
The MVC architecture is very new to me and confusing me greatly. I do not want to have to place the logic to display this username in the controller every time (this is probably the wrong way to do it anyway), so where would I place the code to assign this variable if not in each controller?
Cheers
This is the kind of thing that action helpers were designed for. A full tutorial on them is a bit beyond the scope of SO, but there are several good tutorials available.
Start with the Zend Framework Documentation and then take a look at Mathew Weier O'Phinney's tutorial and also this one by Rob Allen.
The issue with using a base controller for this kind of thing is that the resources are loaded regardless of wether your controller needs them or not, whereas action helpers are loaded only if needed.
I almost forgot the excellent ZendCasts have a video on action helpers.
You want a base controller and to assign that in the preDispatch method:
class MyApp_Controller_Action extends Zend_Controller_Action {
public function preDispatch() {
parent::preDispatch();
Zend_Layout::getMvcInstance()->assign('username', getCurrentUserName());
}
}
Then extend your own controllers with that new class:
class MyApp_Module_ActionController extends MyApp_Controller_Action {
}
Then in your layout view:
echo $this->layout()->username;
First, read the manual, and than try to accomplish something like this:
class BaseController extends Zend_Controller_Action {
public function preDispatch() {
// your logic to show the user name goes here
}
}
class SomePageController extends BaseController {}
class SomeOtherPageController extends BaseController {}
This will most likely solve your problem.
I am about to develop a set of custom Ajax/rss/etc functions, both abstract, and then ones to be used in controllers. I was thinking of separating these methods based on return type.
I have a controller which will be enormous if I don't break down the logic.
I was thinking maybe a module - like
Modules/Admin/AnalyticsController
Modules/AjaxApi/AnalyticsController
Modules/RssApi/AnalyticsController
Any advice is appreciated!
Have you considered using or overriding or creating your own Context Switcher. You can read more here http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
This has the features you need without needing to create new controllers for each action.
You can create your own abstract class with common methods. But in PHP you can to inherit only 1 class. Make your abstract class extends from Zend_Controller Action. Example code:
abstract class AjaxRssEtc extends Zend_Controller_Action
{
// code
}
class Ajaxapi_AnalyticsController extends AjaxRssEtc
{
// code
}
And your controller Ajaxapi_AnalyticsController will have methods from Zend_Controller_Action and your abstract class.