I'm using hostname routes to direct requests to the appropriate modules, so clients.xxx.com routes to the clients module. Now I'm trying to create some phpunit tests and I'm not sure how to dispatch to the proper module?
I've circumvented this issue by adding the default routes onto the router in the test case. Say you wanted to test the foo module, index controller, index action.
/**
* #test
*/
public function indexAction()
{
$frontController = $this->getFrontController();
$router = $frontController->getRouter();
$router->addDefaultRoutes();
$this->dispatch('foo/index/index');
}
You could add the default routes in the setup method to prevent repeating the code.
Related
I'm building a PHP app using Laravel Framework. I need to read some session values on every request and use this values on my controller methods.
How can I achieve this? Where to put this code?
I would like something like the Zend Framework Bootstrap class.
So, you can create a file named, for instance, BaseController.php which extends Controller. Then put your logic in the __construct()
Then, all of your other controllers can extend BaseController and in their __construct() they can perform a parent::__construct(); to make that fire.
The best practice is to use the Laravel Request Lifecycle (https://laravel.com/docs/8.x/lifecycle)
Following the documentation, the best place to place "onLoad" or global code, is on the boot method of the appServiceProvider. For example, if I wan't to set an specific timezone for all my project:
//app/Providers/AppServiceProvider.php
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
date_default_timezone_set('America/Argentina/Jujuy');
}
I don't know the terminology for my problem so I am asking here for the solution since I can't find it by searching.
I am making a simple rest API where I have specified my Routes.php
In my index.php I call the routes:
$routes = new Routes();
$routes->getRoutes();
app/Routes.php
<?php
/* All default application routes go here. Flow specific routes are implemented in the specific flows directory. */
class Routes extends Controller
{
function __construct()
{
/* Inherit stuff from Controller */
parent::__construct();
}
public function getRoutes()
{
/* Routes */
$this->app->group('/api', function () use ($app)
{
$this->app->get('/cron/run', 'Cron:run');
});
}
}
Now I want it to support standalone "modules".
So we add a module and its routes in here:
app/MyModule/Routes.php
Imagine multiple modules in here with their own routes.
Here is the problem.
How can I add routes in all these modules to be included automatically in the application routes?
I do not want to override.
I am using Slim PHP framework if that helps.
You simply need to instantiate each module's Routes class in turn within your bootstrapping process (i.e. before you call run()).
As long as each module defines a unique string for the group's URL (/api in your example), then they won't clash.
I do need to change Zend_Controller_Front and use My_Controller_Front, but I can't figure it out... I have made this:
At My_Controller_Front
/**
* Set singleton instance
*/
public static function setInstance($instance = null) {
self::$_instance = $instance;
}
And at my bootstrap
protected function _replaceZendController() {
Busca_Controller_Front::setInstance(Busca_Controller_Front::getInstance());
return $this;
}
From looking at the code I don't think its possible to have Zend_Application use anything other than Zend_Controller_Front.
When you run a Zend_Application, the following things happen:
Zend_Application::bootstrap() runs
The bootstrap process creates Zend_Application_Bootstrap_Bootstrap which sets up the resource loader and then loads the FrontController resource
The Frontcontroller resource is hardcoded to load Zend_Controller_Front (see Zend/Application/Resource/Frontcontroller::getFrontController())
The only way you may be able to change this behavior would be to register your own resource loader which could intercept the loading of the FrontController resource which would load your Front Controller instead of the Zend Front Controller. Of course you would have to make sure your Front Controller supports every option the Zend Framework one does.
So the question is, why do you need to replace Zend_Controller_Front with your own? Can't you set the appropriate options or create plugins to accomplish your goal?
My controller name is api and I want to create an action update_user_infoAction(), but when I try to create an action zf create action update_user_info Api, I get an error :
Action names should be camel cased.
Here is my terminal command and output:
volition#volition-H61M-DS2:/var/www/Dashboard_check$ zf create action update_user_info Api
Note: PHPUnit is required in order to generate controller test stubs.
An Error Has Occurred
Action names should be camel cased.
Zend Framework Command Line Console Tool v1.11.10
Details for action "Create" and provider "Action"
Action
zf create action name controller-name[=Index] view-included[=1] module
Someone suggested that I use Zend Router but I am a newbie in Zend Framework so please suggest to me how I can create this action.
Note :- I do not want to change my action name
I am trying to create an action with an underscore. I've added this code to my bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$dispatcher = $front->getDispatcher();
$dispatcher->setWordDelimiter(array('.', '-'))->setPathDelimiter('');
}
}
However, when I do the following: zf create controller update_user_info, I still get the same error.
Update: As suggested by #emaillenin
I am Trying this now :
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$frontController = Zend_Controller_Front::getInstance();
$dispatcher = $frontController->getDispatcher();
$dispatcher->setWordDelimiter(array('-', '.', '_'));
}
}
http://localhost/Dashboard_check/public/api/update_user_info
but again, it doesn't work as it works for
http://localhost/Dashboard_check/public/api/updateuserinfo
In your bootstrap, add the following lines of code,
$frontController = Zend_Controller_Front::getInstance();
$dispatcher = $frontController->getDispatcher();
$dispatcher->setWordDelimiter(array('-', '.', '_'));
When you call www.example.com/api/update_user_info
the following function will be invoked.
public function updateUserInfoAction() {
}
Keep your view script for this action in update-user-info.phtml
References -
http://zend-framework-community.634137.n4.nabble.com/Question-about-underscores-in-controller-and-action-names-td656525.html
http://www.zfforums.com/zend-framework-general-discussions-1/general-q-zend-framework-2/underscore-action-name-1204.html
http://zend-framework-community.634137.n4.nabble.com/underscore-in-URL-td648359.html
I try to follow this tutorial, but I can't get it to work:
http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html
I did everything as described, but I don't know how to make it available in my controllers. My filesystem looks like this:
- application
- controllers
- IndexController.php
- modules
- user
- configs
user.ini
- controllers
- forms
Login.php
- helpers
HandleLogin.php
- views
- scripts
login.phmtl
profile.phtml
Bootstrap.php
- views
How do I use the HandleLogin Helper in my IndexController? I really have no idea and I'm looking an trying for more then a day and I almost want to throw my PC out of the window ;). So any help would be appreciated!
Looks like the widget plugin is not called anywhere.
Few things to check:
Do you have a Bootstrap.php file for the module?
Does this bootstrap file has _initWidgets() method?
Does this method call:
$widget = new Module_Widget_Name; // is it callable?
Zend_Controller_Action_HelperBroker::addHelper($widget);
Have you registered widget resource?
public function _initResourceLoader()
{
$loader = $this->getResourceLoader();
$loader->addResourceType('helper', 'helpers', 'Helper');
$loader->addResourceType('widget', 'widgets', 'Widget');
return $loader;
}
Does application.ini contains resources.modules[] = line?
You dont. The point of the tutorial is to create a reusable widget that runs independent from any specific controllers. When the application receives a request, it will run through it's dispatch cycle and trigger the action helper on preDispatch automatically:
Now, let's look at the action helper itself. As a reminder, action helpers can define hooks for init() (invoked by the helper broker each time it is passed to a new controller), preDispatch() (invoked prior to executing the controller's preDispatch() hook and executing the action itself), and postDispatch() (executed after the action and the controller's postDispatch() routine).
The helper will fetch the current controller (whichever that may be for that request) to get the View instance and configure it with the form