codeigniter+HMVC cross module call controller->method - php

I am pulling all my hair off... Have been searching every thread, would appreciate if someone can point me to a working example.
Accroding to the doc: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I can call another module->controller using
modules::run('module/controller/method', $params);
modules::load('module/controller/method', $params);
or
$this->load->module('module/controller');
$this->controller->method();
Problem: the "method()" is never called. only constructor of the controller is called every time.
The objective is to build self-contained MVCs as module and use by other controllers.
But no matter what I do, it only calls the constructor, method is not called.
I started using HMVC a few weeks ago, did I miss something in the doc or it is not used this way?
Here is the setup:
modules
|--ztest1
| |--controller/c1.php
|--ztest2
|--controller/c2.php
class C1 extends MX_Controller {
function __construct() {
parent::__construct();
}
function index () {
Modules::run('ztest2/c2/testc2/');
//Modules::load('ztest2/c2/testc2/');
//$this->load->module('ztest2/c2/testc2/');
//$this->c2->testc2();
}
}
class C2 extends MX_Controller {
function __construct() {
parent::__construct();
echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
}
function testc2(){
echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
}
}
output:
/app/modules/ztest2/controllers/c2.php // C2/__construct
additional note: no error or warning with the script. It just quietly calls the constructor.

Thanks for MC's tip, I finally figured out the cause. HMVC doc indeed lacks some examples for beginner.
For anyone who may find this thread in the future, correct usage here:
to call module01/controller01/method00:
//method 1 CORRECT:
$ctlObj = modules::load('module01/controller01/');
$ctlObj->method00();
//or you could use chaining:
modules::load('module01/controller01/')->method00();
//method 1 WRONG:
modules::load('module01/controller01/method00'); //this will only load contructor
---
//method 2 CORRECT:
modules::run('module01/controller01/method00'); //no trailing slash!
//method 2 WRONG:
modules::run('module01/controller01/method00/');
---
//method 3 CORRECT:
$this->load->module('module01/controller01');
$this->controller01->method00();
I don't understand why method 3 failed when I first try... maybe because I restarted HTTPD?

This HMVC works well for me. I'm working on a project using this HMVC now.
Just edit third_party/MX/Modules.php as shown in this link below and tell me the response.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/pull-request/5/return-error-messages-instead-of-logging/diff

I ran into the same issue. Make sure you check capitalization of your directories and and controllers. It's not case sensitive for differing between the module and controller name.
//In my case the below did not work
$this->load->module('dashboard/Dashboard');
$this->Dashboard->method();
//but
$this->load->module('dashboard');
$this->Dashboard->method();
//worked

After some attempts to achieve call a controller that is not located within any module.
Modules::run('../Controller/method');

I am new at CI as well, and I thought I was having the same issue. Script seemed not to be running. (no html output).
//This did NOT work (did not produce output)
modules::run('module_name/method_name',$data);
// but this DID work??? didn't know why
modules::run('module_name/method_name',$data);
exit();
// turns out you need the echo for output
echo modules::run('templates/login_template',$data);
This may be obvious to many of you- but I wasted two hours searching for an answer.

so According to the documentation they says copy the controller in default controller folder and move to modules controller.
So now how do I run the controller that has been moved to modules when I run its running from the default controller file if removed does not work so how to make it run the controller inside module as a default controller to run.
So Do I need to mention the modules name too in the route

/*echo Modules::run("controller name of a module which you want to call/and its.. function name");*/
echo Modules::run("Second/callit");
or
$this->load->module('Second');
$this->second->callit();
But.. the controller name shold be different .. from one module to another module..
**(parameter passing)**
echo "<hr>";
//echo Modules::run("controller name of a module which you want to call/and its.. function name");
$data="peter";
echo Modules::run("Second/callit",$data);
echo "<hr>";
$this->load->module('Second');
$this->second->callit($data);
echo "<hr>";

Related

TYPO3 add and implement action for news extension

In my Configuration\TCA\Overrides\tx_news_domain_model.php I add option to select field like this:
$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['newItems']['News->gallery'] = 'Gallery view';
In my ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['classes']['Controller/NewsController'][] = 'news_extender';
In my extension
namespace Vendor\NewsExtender\Controller\NewsController;
class NewsController extends \GeorgRinger\News\Controller\NewsContoller {
public function galleryAction()
{
...
}
}
I get an error: An action "galleryAction" does not exist in controller "GeorgRinger\News\Controller\NewsController" So my guess is no override of actual NewsController happens.
My question is if it is possible to add new action to the controller the way I do (['Controller/NewsController'][])? If not, can I achieve this other way? If so provide an example.
The first code must go into a ext_localconf file. Please upload full code to eg github.com to take a look as in general it looks good
I don't know what exactly i did to fix it, but as i did my research i found out that my path to controller was wrong, after changing it i spend few more hours clearing cache from BE and reinstalling extension with no result. After adding line function listAction() {} to my NewsController i finally got an error
Fatal error: Cannot redeclare GeorgRinger\News\Controller\NewsController::listAction() in D:\live_system\typo3\typo3_src\typo3temp\Cache\Code\news\tx_news_controller_newscontroller.php on line 618. After clearing Typo3temp/Cache manually again and removing listActionfrom my controller all works just fine.

CI constructor is failing to load

I have written a core controller in application/core--MY_Controller.php:
class MY_ProtectedController extends CI_Controller{
public function __construct(){
echo("I luv bd1"); //This echo's
parent::__construct();
echo("I luv bd2"); //this one even couldn't reached to
$this->CI=& get_instance();
$this->CI->load->library('permission');
$this->CI->load->library('authentication');
$this->CI->load->model('commonmodel');
$this->CI->load->model('admin/usermodel');
$this->CI->load->library('imagelib');
$this->CI->load->library('facebook');
}
As you could see the first echo is echoed but the second cant get reached because parent::__construct calls CI_Controllers's __construct which fails somewhere in method resolution order(successive calls to other methods in it---I guess). If I keep debugging I think it's gonna take me the whole day excepting I have already wasted two days behind this.
FYI: I'm on EC2 VPS. installed php5.4*,apache2.4*,mysql5.5* CI:2.1.3, php.ini:display_errors=On,
CI: error_reporting(E_ALL)
[But CI's error log is not being written may be for the same reason. I have given log folder 755 perm.]
More Debug: Calling controllers without inheriting from MY_protected also results in the same.
In the matter of course: autoload,library load, view load,helper load ALL ARE FAILING.
Please Help!
Try installing a fresh copy of codeigniter, i.e. Download Codeigniter and Paste only the files you have changed or added.
For instance you most likely added new files to the controllers folder, so move your work in to the, fresh install of codeigniter's, controllers folder. do this for all you work..
I know this is not a proper solution, but at least if you do it this way you can continue working. I can not see with the info you provided, why it would not run anything after parent::__construct(); or even run that line.
Hope this helps.

TinyMVC always shows 'default' controller

I was following documentaion till the end, tested one html template with smarty and then cut it. Then I found out that controllers do not work as expected – whatever name I create in myapp/conrollers, 'hello.php' for example, that contains class described in docs, i. e.
class Hello_Controller extends TinyMVC_Controller
{
function index()
{
echo "Hello World.";
}
function time()
{
echo "The time is now.";
}
}
I can’t show it. So the name of the file is a prefix for the controller class name, all seems to be ok here, but going to /index.php/hello returns what is in 'default.php'. I’ve even tried to change default controller to 'hello' in myapp/configs/application.php by setting $config['default_controller'], but the framework behaves like if it’s always work with the 'default.php'. There is no errors on screen or in logs (I checked twice every option in configs of my web server and interpreter), I totally don’t know what to do with that goddamn piece of crap, I can’t even write on its forum because waiting for ‘administration approval’ for several days.
I had to dig inside of the framework to find an answer. And it is when it checks for a controller file it uses file_exists() which do not respect include path. Googling ‘TinyMVC+file_exists’ gave me link to that topic, where is written that they had fixed it in SVN version.

Codeigniter app returns blank output in CLI

I'm setting up my cron job controller that will only run within the CLI, I've not started with anything built, just in the testing phase with CI's examples. However, when running it I get no output or anything else, just a new line, this is the command I ran:
root#serv$ php /var/www/ci/index.php tools message
root#serv
As you can see in the second line, I get no output, just a new line to run an command but I don't understand why and I cannot debug it. The code contains this:
<?php
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
In my configuration file, the $config['uri_protocol'] is set to AUTO so this does not seem to be the problem.
How can I debug this? What are the options that I may need to deal with?
I also have display_errors on and error_reporting on to E_ALL.
I found the problem, it was a problem with a redirect('domain.com'); exit; I had on an autoloaded library, because it was matching against the domain in a database, that way CLI doesn't serve a domain when it detects, hence I included a redirect('domain.com') with an exit which is why I'm not seeing any output.
I also encountered this while i was playing w/ cli for codeigniter. It took me a while to troubleshoot the issue and found out that I forgot to except my controller on my login model which runs a redirect function.
This may be a silly suggestion, but might as well give it a shot... #lolwut, what if instead of using "echo", maybe you have to "return" the output?
I have an application following this guide: http://codeigniter.com/wiki/Category:Advanced::CronScript running (and producing output). CI was only 1.7.2 when I did so, but it might still hold
(CodeIgniter 2.2.0)
Add a route in /application/config/routes.php
for...
<?php
class Cron extends CI_Controller
{
public function process_dumps()
{
echo "Processing dumps..." . PHP_EOL;
}
}
?>
add...
$route['cron/process_dumps'] = 'cron/process_dumps'
Without this line there was no output on the CLI!
I think you can find the answer in the Codeigniter Wiki.
By calling to the cron.php with parameters controller and function, and then define CRON_CI_INDEX to the refer to the file path of your main index.php.
For example,
php /var/www/ci/cron.php --run=/tools/message

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

Categories