I have an on-going CI v2.0.2 app that was coded by other developers.
I started off by creating a trial controller: `controllers/trial/trial.php'. The code in this controller is:
<h1>controller</h1>
<?php
class Trial extends CI_Controller {
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
}
And the view is in views/trial/trial_view.php. The view has a simple <p>this is the view</p> line.
Now when I visit the URL - http://localhost/ci/index.php/trial/trial all I get is the <h1> tag from the controller. If I remove that tag, nothing is seen, not even the echo statement.
The code base I was given is an exact replica of the app that is being used right now. I've doubly checked to make sure the folder structure is correct too.
What should be going on here? Any config options I should look at?
Update--------
I moved trial.php into the controllers folder, and trial_view.php into the views folder. Made the appropriate changes in the code too. But the result is still the same - only the h1 tag from the controller is displayed when I visit http://localhost/ci/index.php/trial
your action is called index, while you're trying to access controller's trial action, which does not exist.
change it to,
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
try putting trial.php outside trial folder inside controller folder, and get back what happens
try changing
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
to
public function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
BTW try turning on error reporting and see if error is thrown
EDIT
BTW i tested with your code with same setting. It is working in my machine
First off all, stop using the index method to do anything. If your class is called Trial, you need to do this with the index method:
public function index()
{
$this->trial();
}
Then do everything under a method called trial.
Related
Im trying to extend the functionality of the in built hooks by adding some hooks of my own. The documentation on this is pretty scarce so Im posting it here in hopes one of you has done something similar.
I have added a MY_Hooks.php file under the application/core folder as follows
class MY_Hooks extends CI_Hooks {
public function __construct()
{
parent::__construct();
echo "<pre>";
var_dump( $this->hooks );
echo "</pre>";
//this works fine when placed right here
$this->hooks["admin_start"] = function(){
echo "Boosting";
};
// this doesnt work when i simply include the file from the theme directory
if (file_exists(APPPATH.'themes/default/hooks.php'))
{
include(APPPATH.'themes/default/hooks.php');
}
}
}
if you take a look at the comments you can see i have 1 snippet of code which works just fine.
$this->hooks["admin_start"] = function(){
echo "Boosting";
};
and then in the next snippet, I am simply trying to include the same code from a themes directory that i have set up. Which doesnt work. So im scratching my head as to why.
if (file_exists(APPPATH.'themes/default/hooks.php'))
{
include(APPPATH.'themes/default/hooks.php');
}
so its the same code just inside of an include file. Please tell me what Im missing. Thanks in advance.
I have created a module which was downloaded from SocialEngine and uploaded there without making any changes to the source code.
The controller code:
// Controllers/IndexController.php
class Likecounter_IndexController extends Core_Controller_Action_Standard
{
public function indexAction()
{
$this->view->someVar = 'someVal';
}
}
The view code:
// views/scripts/index/index.tpl
Index for skeleton module: Likecounter
someVar : <?php echo $this->someVar; ?>
And the Bootstrap.php code:
// Root of Likecounter(module name) directory
class Likecounter_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
}
BUT NOTHING OUTPUT :(
What's your guess? Why it doesn't print out anything?
Thanks
It will work on the link with /likecounter or whatever you set in manifest.php under routes.
Does your module include necessary files/folders such as /application/modules/Likecounter/settings/manifest.php? If so, can you check if your module is enabled in your package manager (it can be found on yoursite.com/install/manage)?
Next, you may place something like echo "it works"; die(); right in indexAction() of your controller. Then when you visit yoursite.com/likecounter it should show you blank white page with "it works" text.
If not, you should check the end part of the log file which is in /temporary/log/main.log. Hope this helps.
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>";
I have a problem in CodeIgniter, and that is that when an image is not found on the server, the instance of a controller is created (besides the one that called the view).
I know all this can sound confusing, so this is the code to observe what I'm saying. I did this changes to a clean 2.1.0 CI version:
Add a controller to override the 404 error page, I added this one:
// add application/controllers/Errors.php
Class Errors extends CI_Controller {
public function error_404() {
echo 'error';
}
}
// change routes.php
$route['404_override'] = 'Errors/error_404';
Use a controller that isn’t the default one with an unexisting image, I used this:
// add application/controllers/Foo.php
Class Foo extends CI_Controller {
public function index() {
echo '<img src="doesntexist.png" />';
}
}
I couldn’t figure out another way of debugging it, so I created a log to write the events on CodeIgniter.php:
// add on CodeIgniter.php line 356
$path = 'log.txt'; //Place log where you can find it
$file = fopen($path, 'a');
fwrite($file, "Calling method {$class}/{$method} with request {$_SERVER['REQUEST_URI']}\r\n");
fclose($file);
With this, the log that generates visiting the index function is the following:
Calling method Foo/index with request /test/index.php/Foo
Calling method Errors/error_404 with request /test/index.php/doesntexist.png
Which is the problem I have, an instance of the Error class is created.
that is that when an image is not found on the server, the instance of a controller is created
Not really. What I believe is happening is that, since you're using a relative path for the image (and calling it directly inside a controller, which is wrong because you're ouputting something before headers), your browser attach the image directly to the CI url, thus making this request to the server:
index.php/doesntexist.png
Which is (correctly) interpreted by CI as a request to a controller, which doesn't exists, and therefore it issues the error class.
You could do, in your actual code (I'd put the images in a view, though):
echo '<img src="/doesntexist.png" />'
using an absoluth path, or using the base_url() method from the url helper:
echo '<img src="'.base_url().'doesntexist.png" />
This should tell the server to fetch the right request (/test/doesntexist.png) and won't trigger that error.
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.