Zend: Calling an action - php

I'm trying to get my head around the Zend Framework.
I've created a custom route
resources.router.routes.helloworld.route = /helloworld
resources.router.routes.helloworld.defaults.module = default
resources.router.routes.helloworld.defaults.controller = helloworld
resources.router.routes.helloworld.defaults.action = display
In my hellowrold controller class I have changed the indexAction() name to displayAction().
when I try to load the page in a browser I get the following error message:
'script'xxx/display.phtml' not found in path(C:/:blah blah blah)'
What am I doing wrong here?

By default Zend Framework controller actions use the ViewRenderer Controller helper. This helper reads a .phtml file related to the action as the View Script of the related action.
The controller accesses models and in the end passes the result data to view scripts, so view scripts could present the data. This is the "V" in the "MVC" abbreviation.
For your case, you have specified that your default action is named "display" instead of "index".
But I think you have forgotten to create a view script file for this action.
By default the view scripts are located in the APPLICATION_PATH/application/views/scripts directory, with these assumptions:
APPLICATION_PATH is where your application structure and public directory reside in, (for example /var/www/ on Debain Linux or C:\Program Files\Apache Group\Apache\htdocs on Windows).
You are keeping the source code of your application (including your models, controllers, modules, etc.) in the directory called application
If you are having modules in your application and for example your modules are defined in application/modules; then the view/scripts directory is located in each module directory instead of the root of the application structure.
In the above directory, each controller should have a related subdirectory, and each action have a view script file in .phtml extension.
You should create a directory called "helloworld" in there, and then create a file named "display.phtml" in that directory, so the ViewRendere controller helper class could load it and use it as the view of this action.
If you do not want to have a view script, you should prevent the ViewRendere helper from searching for a view script file. to do so, add this code to your action in the controller code:
$this->_helper->viewRenderer->setNoRender(true);
This code tells the view renderer action helper not to search for a view script file.
Please not that all the above are mentioned for a default configured Zend Framework application, but could be changed by configuring your application, resources and helper objects.

Related

Codeigniter how to access views/web pages from third party folders

I am in the process of installing SimpleSAML and in the php library, there is a folder called www that has index.php. According to the docs, there is an admin console within it. However, at the moment I am unable to access it via the url www.website.com/third_party/simplesaml/www/index.php.
I am supposed to use the admin console to generate some metadata so I'm just wondering if it is possible to route to a view from there?
I'm thinking that I create a controller and just hard link $this->load->view('url to www') but I'm not sure if that works.
In controller’s constructor add
include APPPATH . 'third_party/simplesaml/www/index.php';
to include the file in your project.
you can set base path in route file and instead of $this->load->view() you can use renderView() function to access view in codeigniter.

Zend Framework 2: Not being able to access to global vars in namespaced files

I cannot see global vars from an API developed in Zend Framework and I am stuck with that.
The file system is like this:
Project
Site
prepend.php (no namespace)
(many folders of the site) ...
api
index.php
module
usersApi
module.php (namespace UsersApi)
src
usersApi
controller
AbstractRestfulJsonController.php (n: UsersApi\Controller)
usersController.php (n: UsersApi\Controller)
core
config
config.php
config.xml
The Apache is configured to execute a prepend file which aim is to load the config.php file, which loads the config.xml so it means that before every request is executed, it always loads all the configuration and then retrieves from core_framework_config::getConfig()->{{key}}.
The problem is that I am able to get the config everywhere but in the web services. In fact the index.php inside api folder can access the core_framework_config and all their vars but when I enter in a namespaced file (e.g. userController) I get an error executing it both inside and outside the class, but always in namespaced files. The error is that the UsersApi\Controller\core_framework_config method does not exist. Can anyone help me?
UsersController.php:
namespace UsersApi\Controller;
use UsersApi\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class UsersController extends AbstractRestfulJsonController
{
public function get($id) { // Action used for GET requests with resource Id
// I MUST RELOAD THE CONFIG FILE BECAUSE I CANNOT ACCESS TO CONFIGURED FILE
$items_per_page = core_framework_config::getConfig()->items_per_page; // this fails with the error mentioned before
}
}
I fixed it by adding the slash at the beggining: \core_framework_config

laravel 4 create module and put controller , module and view inside module

I have worked on yii framework before and I had possibility to create module folder and inside this put eg: news module which was included controllers, views and module
I'm new in laravel and trying to create same thing MODULE
i tried the following inside routing
Route::get('/news','/modules/news/controllers/NewsController#index');
file exist but i'm getting
ReflectionException
Class /modules/news/controllers/NewsController does not exist
why ? what i'm doing wrong ?
The Route::get() function is looking for a (auto)loaded Class, not for a file on the disk to load, which is why you're getting these errors.
It's more Laravely (Laravelish?) to include:
Controllers in the /app/controllers/ directory
Views in /app/views/ directory
Models in the /app/models/ directory
And if you are starting out with Laravel, this might be the best way to get started. The autoloader knows where to look for your classes then, and everything gets handled automatically for you.
With the NewsController situated in /app/controllers/ you can do this:
// no need to start the route location with a slash:
Route::get('news', array('uses' => 'NewsController#index'));
You can "package" up functionality using Laravel's Packages, but it would be better to check out the excellent docs and then come back with specific questions..
Put your Laravel controllers in app/controllers, since that directory gets autoloaded, and it is where Laravel expects controllers to be.
Then you can use routes like this (example straight from the docs at http://laravel.com/docs/controllers#basic-controllers)
Route::get('user/{id}', 'UserController#showProfile');

HMVC and Views in folders (Codeigniter)

I am using Tank Auth library in Codeigniter with HMVC and the entire tank auth mvc files are in its own module called 'auth'. tank auth loads a view (domain.com/application/modules/auth/views/auth/login_form.php) found inside a folder (auth) using:
$this->load->view('auth/login_form', $data);
As far as I know the above code will load login_form.php inside the auth folder properly without HMVC. However with HMVC, I need the following code to get the view to load:
$this->load->view('auth/auth/login_form', $data);
Is there a setting that we should change so we dont have to refer to the view file by (module name)/(views folder name)/(view filename) ? Or is this perfectly normal and most people does it this way?
It seems troublesome that I have to add the module folder name 'auth' to every view() function call, and change all of them should I change the name of the module folder.
Assuming you're using Modular Extensions - HMVC:
If you have auth set up as a module already, you can just call:
$this->load->view('login_form', $data);
The file /views/login_form.php will be loaded from within the current module. This applies to models, language files, libraries etc. Think of the module as its own application, this is what you would normally do.
Additionally, to load a file from another module or a controller outside the module's directory, you can use $this->load->view('auth/login_form');
If the file is not found, it will check the other module paths including the default directory. This may or may not be the way other HMVC packages work, I'm not sure - but it's the way MX works.

Zend Framework command to remove an action

I am new to Zend Framework, and i am attempting to remove an action using ZF command line tools. However, i am not able to do so. Basically, there's a function to an action in my controller that i wish to remove. I created the action using the following command:
Action
zf create action name controller-name[=Index] view-included[=1] module
Is there a command to remove it?
There is no command in Zend_Tool for that. You need to do that manually, by removing the action method in the controller and deleting the view script. You have also to edit the .zfproject.xml in the root of your project.
If you remove any controller or action manually, you have to also edit .zfproject.xml file on the root of your project, otherwise it will not allow you to create that controller or action in future again with zend tool.
Go to your application root and edit .zfproject.xml file. This is the file, where zf command will create each controller and action log. remove those actions from there and from controller files and also the views.

Categories