I have installed cakephp in document root folder and renamed it to todo. So the complete path is (C:\localhost\todo). I am able to run the index.php perfectly fine (all tabs are green).
I created the 'todo' example application from the book 'Beginning CAKE PHP - Novoice to Professional'. I keep getting the error 'MISSING CONTROLLER' even though I have the items_controller.php file. I am thinking for some reason the application does not know where the controller file is present.
The complete error is :
Missing Controller Error: ItemsController could not be found. Error:
Create the class ItemsController below in file:
app\Controller\ItemsController.php
<?php
class ItemsController extends AppController {
}
I have .htacess and index.php files in respective folder
Can someone please help.
Controller class names are plural, CamelCased, and end in Controller. So your controller name should be ItemsController.php not items_controller.php. See here for more info on Controllers.
class ItemsController extends AppController {
//class code here
}
Related
So I created a TestController file inside the frontend/controllers directory and then created a view/test directory with an index.php file inside it. my code for the TestController is this
<?php
namespace app\frontend\controllers;
use yii\web\Controller;
class TestController extends Controller{
public function actionTest(){
return $this->render('index');
}
}
my index file only contains an echo of hello world.
my question now is, how to run this in the browser ?
I tried in the url
http://localhost/myapp/frontend/test/
I got an error
Not Found
The requested URL /myapp/frontend/test/ was not found on this server.
I also tried this
http://localhost/myapp/frontend/test/index
still won't work. so how ?
You try just
The fist test controller and second one view
http://localhost/myapp/frontend/test/test
OR
http://localhost/myapp/frontend/test/test/index
I am aware of the other questions on the platform for this issue however I am having a very unusual issue with this.
I have a model Company_Model.php which is being autoloaded in the autoload.php and the Class is built like this:
Class Company_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function foo()
{
echo 'bar';
}
}
However I am still getting this error when I load the page:
I am running on Apache 2, PHP 5.5.9 on Ubuntu 14.04, I cannot find an error log for this issue and now I am baffled, any help would be gratefully recieved.
I have check the uppercasing and all of the other tips from StackOverflow but still no joy.
Edit
Auto load code
$autoload['model'] = array('company_model');
When you use codeigniter 3 you have to make sure your class names and file names only have the first letter upper case as explained here
Class Naming Style Guide
Filename Style Guide
<?php
class Company_model extends CI_Model {
}
And file name then should be Company_model.php
$autoload['model'] = array('company_model');
If in sub folder
models > sub folder > Company_model.php
$autoload['model'] = array('subfolder/company_model');
If need to call it on controller only
$this->load->model('company_model');
$this->company_model->function();
Sub folder
$this->load->model('subfolder/company_model');
$this->company_model->function();
Make sure your class name and file name should be same and first Letter should be Capital
Example
class Login extends CI_Model{
//some code
}
above code, you can see the class Login first letter is capital.same name you have save of your model file name like Login.php in the model directory of your project folder
Try Changing the class name Company_Model to Company_model and also change your file name Company_Model.php to company_model.php
Hello guys i created Entity folder inside application/model/. In Entity folder i created file Mj_user.php and give class name as same Mj_user.
but when i try to access that class using
$User= new Entity\Mj_user;
it's giving me error
Fatal error: Class 'Entity\Mj_user' not found in C:\xampp\htdocs\project\application\controllers\user.php on line 15
what should i do Please help me..but when i remove Mj_ then put only file name as User.php. Its working properly..Please help me
When creating a new class you have to be careful, the name of the class must be:
class Mj_user extends CI_Model {
function __contruct()
{
partent::construct();
}
}
The file must be mj_user.php and for using this model you must load it before start using it
it can be preloaded for all in the configuration or you can load it whenever you need it like this
$this->load->view('mj_user');
And in the error seems that is looking for a file called user.php and should be mj_user.php
The error I'm getting is that the controller doesn't exist even though I know it does, here's the code.
Route.php
Route::get('mdpay/template', array("uses" => "templateController#index"));
templateController.blade.php
class templateController extends BaseController {
public function index()
{
echo "made it";
}
}
Why might I be getting this error: Class TemplateController does not exist
================= UPDATE: ==================
Ok, so I've created the correct route, renamed my file, and corrected the class name and I'm still coming up with that error.
File Names:
templateController.php
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// app/views/myView.blade.php
echo "hello";
}
}
My route is:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Still receiving Controller Doesn't Exist error. All my other controllers (3 others) are working except this one.
If you are using the standard composer classmap autoloader you need to composer dumpautoload everytime you create a new file.
So to create a new controller with the standard composer setup given by Laravel:
Create a new file in app/controllers named TemplateController.php
Open up terminal and run composer dumpautoload
As previous users have said, only view files should end with .blade.php.
If you're using Laravel 8, add this line to your RouteServiceProvider.php (you can search it by using CTRL + P):
protected $namespace = 'App\Http\Controllers';
This solved the issue for me.
It should be:
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// return "made it"; // or
// app/views/myView.blade.php
return View::make('myView');
}
}
Route for that:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Use blade in a Blade view, i.e: myView.blade.php basically stored in app/views/ folder. Read more about blate template on Laravel website.
Controllers live in the app/controllers directory and should remain there unless you have your own namespaced structure.
The reason you're getting a Class TemplateController does not exist is because it doesn't, firstly, your class is called templateController and secondly, it exists as templateController.blade.php which wouldn't be loaded in this way.
Blade files are for views, and only views within app/views or a custom views directory should end with .blade.php.
Create the file app/controllers/TemplateController.php and add the following code to it.
class TemplateController extends BaseController {
public function index()
{
echo "made it";
}
}
Now on the command line, run the command composer dumpautoload and change you route declaration to:
Route::get('mdpay/template', array('uses' => 'TemplateController#index"));
Now it should all work.
In case you're using Laravel 9 and the error is like Illuminate\Contracts\Container\BindingResolutionException and Target class <controller name> does not exist. when trying php artisan route:list on terminal.
This is the setup that I do:
Add protected $namespace = 'App\\Http\\Controllers'; to RouteServiceProvider.php
Add 'namespace App\Http\Controllers;' to the controller file.
Do php artisan optimize on terminal
(Just to make sure the route already there) Do php artisan route:list again on terminal, and the controller route should be displayed.
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.