Home > front end >  codeigniter setup can't access controller via url
codeigniter setup can't access controller via url

Time:06-26

Firstly i am new to codeigniter i did follow the steps of the docs and some yt videos but I can't seem to get codeigniter to work properly here is my issue

1. My SetUp
Server: Xampp :: localhost
My folder htdocs/crm/code files here

I changed my xampp 'httpd.conf' from

DocumentRoot "C:/xampp/htdocs/"
<Directory "C:/xampp/htdocs/">

to

DocumentRoot "C:/xampp/htdocs/crm/public/"
<Directory "C:/xampp/htdocs/crm/public/">

so that i can access codeigniter via http://localhost/ rather than http://localhost/crm/public

and i changed app\Config\App.php base url etc....

when I go to localhost/ it shows me the codeigniter welcome screen same with localhost/index.php but when i try to type localhost/home it says ->

404 - File Not Found
Can't find a route for 'get: home'.

but home is a controller in my folder app\Controllers\Home.php

home.php

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('welcome_message');
    }
}

It should be accessible via the url right? can anyone tell me what i might have done wrong? did i not configure something correctly.

Please understand i am new to codeignter not php but just the framework its my first time using it and if i need to supply more info or docs please comment and i will edit my Q

Thank you and have a great day :)

CodePudding user response:

You need to setup routes in Codeigniter 4. app/Config/Routes.php file. There you may find this for new installation:

$routes->get("/", "Home::index");

Here you may define more routes like:

$routes->get("/hello", "Home::hello");

In Home controller you may say:

public function hello()
{
    return "Hello";
}

CodePudding user response:

NOTE: This applies only to ver `4.2.1` as I have not tested the other versions

First
you have to go to app/Config/Routes.php then uncomment property setAutoRoute(false) and set it's value to true not false then go to app/Config/Feature.php and change the property $autoRoutesImproved to true

When no defined route is found that matches the URI, the system will attempt to match that URI against the controllers and methods when Auto Routing is enabled. - Codeignter docs

Secondly
A controller method that will be executed by Auto Routing (Improved) needs HTTP verb (get, post, put, etc.) prefix like getIndex(), postCreate().

Example:

<?php

namespace App\Controllers;

class Helloworld extends BaseController
{
    public function getIndex()
    {
        return 'Hello World!';
    }
}

Please note this is from my experience and according to Codeignter documentation, you can find the full article here: Auto Routing (Improved) and URI Routing

  • Related