I am using REST_controller in HMVC (CI3.1). - codeigniter

It is working correctly with MX_Controller.It is throwing error as 'Fatal error: Class 'CI_Template' not found' when using REST_Controller.Any help will be appreciated.

I'm not entirely familiar with the inner workings of Codeigniter's third party add-on HMVC. However, the code base that I inherited is using it.
I ran in to the same issue when installing restserver, https://github.com/chriskacerguis/codeigniter-restserver which is what I assume you were also doing.
REST_Controller.php has this
abstract class REST_Controller extends \CI_Controller {
While I have never been able to get CI_Controller to work with this codebase, I now believe it is because of this HMVC. For those not familiar, HMVC is Hierarchical Model View Controller.
Module Controllers can be used as normal Controllers or HMVC
Controllers and they can be used as widgets to help you build view
partials.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I did get REST_Controller.php working by changing the line to
abstract class REST_Controller extends \MX_Controller { // \CI_Controller {

Related

Codeigniter 4 - controllers won't work unless I add them directly to Routes.php

Can anyone tell me:
Do I have to declare all of my controllers in Routes.php in Codeigniter 4?
I can't seem to get a controller to work unless I add it directly to the "Routes.php"
I have created my controllers properly and the Home controller is working after install and setup.
If I add the controller My_page.php :
<?php
namespace App\Controllers;
class My_page extends BaseController{
public function index(){
echo "Controller 'My_page' -> function index() ";
}
}
?>
I get a
: "404 - File Not Found
Sorry! Cannot seem to find the page you were looking for."
If I now add the controller to the rout - i.e.:
$routes->post('my_page', 'My_page::index');
Then my controller works properly and I get the "Controller 'My_page' -> function index() " when I visit www.mydomain.com/my_page
I have also tested:
www.mydomain.com/index.php/my_page
and this makes no difference.
I am using the .htaccess that comes with the download. I have updated the base URL to www.mydomain.com/
The documentation is confusing to me - https://www.codeigniter.com/user_guide/incoming/routing.html#setting-routing-rules ;
it sounds like they are saying have to declare all classes with routes?
Why are my controllers not working without declaring them specifically in Routes.php?
And am I misunderstanding 'setAutoRoute(true)' it also does not seem to work - I expect that I can turn this on and simply create my controllers pretty much like in CI3?
If you don't enable auto-routing you most certainly need to add all routes which you are allowing, anything else will fail with error 404. As #parttimeturtle mentioned - autoroute it is disabled by default since 4.2.
So in short - Yes, you need to add all controllers, their functions and the appropriate http methods. (That includes CLI routes as well)
You can use $route->add(), which will allow all http methods, it's however more secure to explicitly set them with their methods.

Codeigniter: How can i Call an external controller in a controller located in controllers folder

I am working with codeigniter in php and my question is that How can i Call an external controller in a controller located in controllers folder.
//this file is Controller2.php in controllers folder
class Controller2 extends CI_Controller
{
function one()
{
// Some code goes here..
}
}
//and now this file is Controller1.php which is also in controllers folder
class Controller1 extends CI_Controller
{
function one()
{
// I want to load Controller2 here
}
}
Please instruct me as i've wasted so much time during googling.
Thanks in advance..
Kamran
You can, but not with the default functionality, to use something similar you should use HMVC extension for CodeIgniter: Link
There are plenty examples and a good thread about the extension.
You can't, or rather you shouldn't. Any functionality that you need to use in two different controllers should be moved to a library or helper file.

Error in extending CodeIgniter Controller [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to extend the CodeIgniter controller. I have created MY_Controller.php file, the content of the file is as following:
<?php
class MY_Controller extends CI_Controller
{
function generate_key()
{
$n = rand(10e16, 10e20);
return base_convert($n, 10, 36);
}
Now I create my controllers by extending MY_Controller instead of CI_Controller. Following is an example of a controller:
class Member extends MY_Controller
{
public function index()
{
$this->load->view('welcome');
}
I have placed the MY_Controller.php file in the Application/libraries/ folder. But when I load the application, I get the error:
Fatal error: Class 'MY_Controller' not found in path\to\application\controllers\member.php
Can someone tell me what i am doing wrong? Thanks.
Edit: I'm using CodeIgniter 2.0.2
I had been this issue:
`Fatal error: Class 'MY_Controller' not found ...`
publishing to my remote Linux server (DreamHost) but not on locally on Windows. The issue was Linux being case sensitive: I had MY_Controller.php with a lowecase c and it wasn't autoloading.
Just in case anyone else has the same issue :)
You need to create MY_Controller.php in application/core. I tried what you have setup and got the same problem. Moving the custom controller to core solved the problem.
Hope it helps!
I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching]. Note:- In localhost I didnt have any error.
In extended controller I Use
class Home extends MY_Controller{
function __construct() {
parent::__construct();
}
}
even I got the error.
After changing my file name to MY_Controller it started to work well.
MY_contrller places in application/core folder.

Fatal Error in CodeIgniter

I am facing a Fatal error:
Class 'Controller' not found in D:\wamp\www\CodeIgniter\application\controllers\blog.php on line 3
I am typing write and i watched so many tutorials they are also typing beginning code like this i tried a lot to find out but got no reason
Are you using CI 2.O? Did you by any chance write the following in your controller:
class Blog extends Controller { ... }
If you're using CI 2.0, you should use the following:
class Blog extends CI_Controller { ... }
You might have been watching outdated tutorials for CI 1.7.
Reference: http://codeigniter.com/user_guide/general/controllers.html
Make sure to follow the userguide along with whatever tutorials you're following; there's some changes in 2.0 which you should be aware of. You can still follow these tutorials though, just keep your eyes open and compare with the userguide.
What version are you using? if you're using the latest (2.0.2), make sure you use CI_Controller when extending your controller.
Seeing you named the file blog.php, your controller should be looking like this
Class Blog extends CI_Controller {
function index()
{
// your code...
}
}

how to get intance of CI in codeigniter's Exception library

i just want to run $CI=&get_instance in Exception library of codeigniter. but its not running there. while the other Classes in Library folder are able to create such instance, but the Exception is not. the reason i want to do this is , i want to use this instance to load a view page in show_404(){} method.
thanks in advance
Are you thinking about extending CI's Exceptions library? if so, you may want to do
<?php
class MY_Exceptions extends CI_Exceptions {
// your code here
}
then you don't need to do $CI &= get_instance();

Resources