Yii - Inheriting From Custom Controller Class - Not Found - php

class SomeController extends Controller
{
public function actionIndex() {
echo 'This is some controller';
}
}
class AnotherController extends SomeController
{
public function actionIndex() {
echo 'This is another controller';
}
}
This works:
index.php?r=some
but ...
index.php?r=another
says:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Both of the files are in
test\protected\controllers\
BTW in the past I also tried using the Gii Controller Generator with "SomeController" as the base class...
It said:
The controller has been generated successfully. You may try it now.
Generating code using template
"C:\xampp\htdocs\yii\framework\gii\generators\controller\templates\default"...
generated controllers\YetAnotherController.php
generated views\yetAnother\index.php
done!
When I clicked on "try it now" it also said:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory

Edit:
Classes inside protected/controllers are not autoloaded, therefore you'll have to import the parent class file before extending from it:
In AnotherController.php:
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
// ...
}
Incase you need to access the base class from url also, you can use the above method. Otherwise you can put your base class inside protected/components as you have already figured out.
Yii autoloading works only when you have the same name for the file as the class that the file contains. Meaning class SomeController should be within SomeController.php file.
Make those changes and it should work.
A helpful wiki: Understanding Autoloading Helper Classes and Helper functions.
Guide link:
Class files should be named after the public class they contain.

To extend any class just go to the config file and add the class in the import section
'import' => array('application.controllers.SomeController')
this will make it available in the entire application without importing explicitly.

Related

How to include a function file in symfony2 in controller

Hi I have a file called payment.php it contains some functions related to payments and some multiple classess. So I want to include that file in my symfony2 Controller to access its all methods and classless. I am trying it as follows:
//File location is in src/AppBundle/Controller/payment.php
namespace AppBundle\Controller;
require_once __DIR__.'/./payment.php';
//My controller
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
But I am not able to access the file and its methods.
I am using this approach because keeping it in /vendor directory it also not able to access because this file contains multiple classless in same files.
Please advice me how can I access this file in my controller.
thanks in advance
If paymant.php have classes you need to make instance of that class to call method from it, that's like basic OOP stuff.
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
First of all, where is the method in this controller where you want to call your method? Then, why you are calling payment on $this if it comes from diffrent class. It should be something like this
class ApiServicesController extends Controller
{
public function indexAction()
{
$somePaymentClass = new SomePaymantClass(); //whatever class you want from payment.php
$somePaymentClass->payment(array('txnId'=>1112548));
}
}
But iI strongly recommend to use it as a service and put it in some autoloader namespace.
You have to make a Payment class as a service and then you can use all functions of Payment class in controller. Please refer this document.
http://symfony.com/doc/current/service_container.html

Fatal error: Class not found in codeigniter

I have created a model Admin_Model under Application\core directory of code igniter. I put all basic database operations under it. When I try to extend my models who are under Application\model directory, it throws error.
Fatal error: Class 'Admin_Model' not found in <path to root>/application/models/new_model.php on line 3
Should I miss any configuration?
Thanks all for your efforts.
I find a solution.
By default CodeIgniter have a setting in its config file.
$config['subclass_prefix'] = 'MY_';
I just replace 'MY_' with 'Admin_' everything works fine.
$config['subclass_prefix'] = 'Admin_';
More appropriate solution is
Put class file in libraries folder
Add following code to config.php
function __autoload($classname)
{
if(strpos($classname,'CI_') == 0)
{
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file))
{
#include_once($file);
}
}
}
That's All
This may also be helpful, if you still have problems with __autoload to use class from libraries. it's work for me, when I use class library tobe parent class at controller.
PHP 5 >= 5.1.2, PHP 7
this is mycode at application/config.php
spl_autoload_register(function ($classname){
if(strpos($classname,'CI_') == 0){
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file)){
#include_once($file);
}
}
});
Extending Core Class
If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:
The class declaration must extend the parent class.
Your new class name and filename must be prefixed with MY_ (this item is configurable. See below.).
For example, to extend the native Model class you'll create a file named application/core/MY_Model.php, and declare your class with:
class MY_Model extends CI_Model {
}
Note: If you need to use a constructor in your class make sure you extend the parent constructor:
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
I setup my environment on a Windows machine. Then after move to production using Ubuntu, I had the class not found error.
I discovered my files extension were PHP (capital letter). Linux is case sensitive, so the file name need to be lowercase php.

parent controll in Codeigniter 2.2 project not found

In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.

CodeIgniter - is it possible to create my own controller in any other name

I have created custom controller called "MY_Controller.php" in Application/core, and successfully invoked by inheriting through application controller.
//application/core
class MY_AdminController extends CI_Controller {
function __construct() {
parent::__construct();
}
}
//application/controllers
class User extends MY_AdminController {
public function __construct(){
parent::__construct();
}
}
It works fine. I just changed my file name from "MY_Controller.php" to MY_AdminController.php, and following same class name, but it is throwing following error,
Fatal error: Class 'MY_AdminController' not found
As per the documentation, Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library, then why its throwing error...!!!
Go to your config.php and change
$config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'MY_Admin';
Expanding on Patel,
the issue is that MY_ is the prefix to the original core files.
Controller, Model, View etc.
MY_ will be used to seek the name of the controller, for example, MY_controller searches for CI_controller.
You cannot load random names using the MY_prefix. you use MY_ to extend the already existing names.
I think the problem is with the class name, you may have not changed the class name from MY_Controller to MY_AdminController
You can use custom classes. But CI only loads the classes with the class prefix in the config file (e.g. MY_). So I explained how it works and created a workaround to load classes automatically. You can find it here https://stackoverflow.com/a/22125436/567854.
Hope this helps :)
If you want to include all the files that are in your core folder. Then write the following code in end of config.php file.Path to file is application/config/config.php
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/' . $class . EXT );
}
}
By using this you can create multiple classes.

Extending The Controller Class in CodeIgniter

I have class MY_Controller extends CI_Controller and common logic for big profile section, so I'va tried to create class Profile extends MY_Controller with common logic for profile section and all class related to this section should extends this Profile class as I understand right, but when I tried to create class Index extends Profile I recieve an error:
Fatal error: Class 'Profile' not found
CodeIgniter tries to find this class in index.php which I am running.
Where is my mistake? Or maybe there is anoter better way to mark out common logic?
I take it you have put your MY_Controller in /application/core, and set the prefix in the config.
I would be careful about using index as a class name though. As a function/method in Codeigniter it has a dedicated behaviour.
If you then want to extend that controller you need to put the classes in the same file.
E.g. In /application core
/* start of php file */
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
class another_controller extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
/* end of php file */
In /application/controllers
class foo extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
or
class bar extends another_controller {
public function __construct() {
parent::__construct();
}
...
}
I found this page on Google because I had the same problem. I didn't like the answers listed here so I created my own solution.
1) Place your parent class in the core folder.
2) Place an include statement at the beginning of all classes that include the parent class.
So a typical controller might look like this:
<?php
require_once APPPATH . 'core/Your_Base_Class.php';
// must use require_once instead of include or you will get an error when loading 404 pages
class NormalController extends Your_Base_Class
{
public function __construct()
{
parent::__construct();
// authentication/permissions code, or whatever you want to put here
}
// your methods go here
}
The reason I like this solution is, the whole point of creating a parent class is to cut down on code repetition. So I don't like the other answer that suggested copy/pasting the parent class into all of your controller classes.
It is possible with Codeigniter 3. Just including the parent file is enough.
require_once(APPPATH."controllers/MyParentController.php");
class MyChildController extends MyParentController {
...
All classes you are extending should live in application/CORE directory so in your case both My_Controller and Profile should live there. All "end point" controllers will live in application/controllers folder
UPDATE
I stand corrected. Extended classes should live in the same file. #Rooneyl's answer shows how to implement
After some struggle with version 3 and this issue I decided this was not a bad solution...
require_once BASEPATH.'core/Controller.php';
require_once APPPATH.'core/MYCI_Controller.php';
to add this second line where the first exists in the system/core/CodeIgniter.php
[If it's not too late, I recommend strongly against php and/or CodeIgniter.]

Categories