Conroller name in url in codeigniter 4 is not working - php

enter image description hereWhen I use this
Localhost/CI4/public
OR
Localhost/CI4/public/index.php
It loads and shows default page with default controller, but when I use
Localhost/CI4/public/index.php/Home
OR
Localhost/CI4/public/index.php/Home/index
It gives *`404 error File Not Found - Controller or its method is not found
1
Routes. Php file
2

Assume my controller name is ContactController and the method is index. And I'm accessing it with the URL contact
In config/route.php
$routes->get('contact', 'ContactController::index');
In Controller
namespace App\Controllers;
class ContactController extends BaseController
{
public function index(): void
{
echo "call me";
}
}
To access this just use
http://localhost/contact
Nothing to do with public or index.php

Related

How can I fix this "Object not found!" error in CodeIgniter-4?

I have checked the controller name and method name but still it says "The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again."
I have tried this url's given below and got this error:
http://localhost/framework/index.php/helloworld
http://localhost/framework/helloworld/index
File under Controller name is: Helloworld.php
<?php namespace App\Controllers;
use CodeIgniter\Controller;
class Helloworld extends CI_Controller
{
public function index()
{
echo 'Hello World!';
}
enter image description here
Change
class Helloworld extends CI_Controller
to
class Helloworld extends Controller
in CI4 CI_Controller renamed as Controller
If You are trying Codeigniter 4 without using htaccess you should call like
http://localhost/framework/public/helloworld
Or you should run the Codeigniter using this Command
php spark serve
After that go to browser check http://localhost:8080
You should learn the basics of codeigniter 4 From here
How to Use codeigniter 4
Hope this Helps
Please try after performing below changes -
Remove use statement.
Extend BaseController as CI_Controller is not available in CI4.
Return what you want to show on the screen.
<?php namespace App\Controllers;
class Helloworld extends BaseController {
public function index() {
return 'Hello World!';
}
}
In the the majority of comments, it was stated that all you had to do was to rename your CI_Controller to plain simple ole Controller ( as per the CI 4 User guide ) and what you declared in your "use".
So you would have
<?php namespace App\Controllers;
use CodeIgniter\Controller; // This is what you are "use"ing
class Helloworld extends Controller { // And this is where you are "use"ing it
public function index() {
echo 'Hello World!';
}
}
See the difference?

How can i access my controller located in "folder_name/controller_name" in codeigniter 4?

My Master controller located in "admin" folder. View image
here
namespace App\Controllers\admin;
use CodeIgniter\Controller;
class Master extends Controller
{
function __construct(){
helper('url');
}
public function index()
{
$data["content"]="view_home";
echo view('template/template', $data);
}
}
In my Routes.php i added this
$routes->get('admin/master','Master::index',['namespace','App\Controllers\admin']);
when i access the page in the browser i get this error
404 - File Not Found
Controller or its method is not found: {0}::{1}
What am i missing?
My silly mistake when setting up the route. I've put a "," instead of "=>". See the correct route below.
$routes->get('admin/master','Master::index',['namespace' => 'App\Controllers\admin']);

how to run a new controller under frontend directory in yii 2?

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

Yii2 Adding new console controller

Why yii2 throw exception, when I try use console controller? Code:
<?php
namespace app\commands;
use yii\console\Controller;
class Hashtag extends Controller
{
public function actionIndex($search = 'test')
{
echo $search;
}
}
Controller located in: app\commands\HashtagController
When using terminal: php yii hashtag
Exception 'yii\base\UnknownClassException' with message 'Unable to find 'app\commands\HashtagController' in file: /var/www/html/yiitask/yii2/commands/HashtagController.php. Namespace missing?'
in /var/www/html/yiitask/yii2/vendor/yiisoft/yii2/BaseYii.php:291
Other controller in this folder that was created before, working well.
Your namespace is wrong set the namespace for console controller properly eg: (depend where you have you console controller directory)
namespace app\console\controllers;
then could be is missing controller
class HashtagController extends Controller
{
instead of
class Hashtag extends Controller
{

Redirect action to controller in different namespace

I have middleware that uses a redirect to call a controller which then displays a view.
public function handle($request, Closure $next)
{
redirect()->action('Full\Namespace\To\Controller\ErrorController#fourOhThree');
}
I also have this as a route. When I follow the route the view is displayed fine. When I try and redirect using action and pass the namespace of my controller, laravel tries to find the controller in the base app. I get error
Action App\Http\Controllers\Full\Namespace\To\Controller\ErrorController#fourOhThree not defined.
When the controller is located at
App\Vendor\Myname\Mypackagename\Controllers\ErrorController#fourOhThree
I have namespaced my controller correctly as far as I can tell as it matches the other namespaced controllers in this directory. This is the only controller I am trying to call from an action.
ErrorController.php within App\Vendor\Myname\Mypackagename\Controllers
namespace Full\Namespace\To\Controller;
use App\Http\Controllers\Controller;
class ErrorController extends Controller
{
public function fourOhThree()
{
return view('...');
}
}
I think I am doing something wrong in how I am passing the namespaced controller to the action method.
Try adding a '\' in front of the qualified name.
action('\Full\Namespace\To\Controller\ErrorController#fourOhThree')

Categories