Module load breaks view load (old code) - php

I'm trying out codeIgniter and the Modular Extension. So far I followed the installation instructions from wiredesignz on bitbucket. Now I'm trying the tutorial HMVC: an Introduction and Application from NetTuts.
I'm doing this with the actual version of CodeIgniter 2.1 and the actual version of the Modular Extension (downloaded the .zip version from bitbucket).
All works fine until I'm trying to run a method from a controller of another module.
The setup in short is, that there's two modules (site & login). Within the site's controllers there's site.php (/modules/site/controllers/site.php) with a method that checks if a user is logged in. This method would exit the script execution if accessing the site without being logged in. As this method logically belongs to the login module, the author suggests moving it there. So it then is moved to /modules/login/controllers/login.php.
The problem now is, how to access the method of the login module from within the site module. The slightly adjusted code from the tutorial:
// modules/site/controllers/site.php
function __construct()
{
// parent::Controller();
// replaced with:
parent::__construct();
// modules::run('login/is_logged_in');
// replaces with:
$this->load->module('login')->is_logged_in();
}
Like this I'm getting an error:
Unable to load the requested file: logged_in_area.php
The method in question is inside the site module as well:
// modules/site/controllers/site.php
function members_area()
{
$this->load->view('logged_in_area');
}
The Script executes right until the load->view line and produces the error. There's no problem accessing the logged_in_area.php with running the is_logged_in method from within the site controller with the line:
$this->is_logged_in();
Any ideas?
edit:
the application tree:
/application
/...
/modules
/login
/controllers
login.php
/models
/views
login_form.php
signup_form.php
signup_successful.php
/site
/controllers
site.php
/models
membership_model.php
/views
logged_in_area.php
PS: How can I get more INformation about the error? CodeIgniter seems to be very reserved about error output ...

As stated in the documentation here to load a view from within another module, you need to use an extended MX controller and a method:
<?php echo Modules::run('module/controller/method', $param, $...); ?>

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.

CodeIgniter index.php 404

I am using CodeIgniter for both frontend and backend. My backend isn't really that complex so it didn't warrant a different installation of CI. What I have now is a sub directory in controllers cms where I have all my backend controllers including a backend Index which extends MY_Backend core. Now I'm working on getting my front end up and running and have come across a problem if I have an Index file in the main controllers directory that extends MY_Frontend core. And try to access it via localhost or localhost/index I get a 404 page. If I change the name and subsequently the class name to Homepage I can access it via localhost/homepage.
Is this possibly due to having an Index file in the cms sub-directory? Otherwise, what is the issue? Here is my directory structure:
As at the base of it all controllers extend CI_Controller, there are 3 naming restrictions in place for controllers:
CI_Controller
Index
Default
Using any of these would cause a problem in some shape or form. I'd suspect this would be why your controller works fine named as Homepage, but not as Index.
Source: https://www.codeigniter.com/user_guide/general/reserved_names.html

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

Zend: Calling an action

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.

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.

Categories