0

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 :)

4
  • 1
    I'm not sure which version you are using but try to add yourdomain/index.php/controllername. Then it may work. The reason is simple, MVC frameworks start every request with index.php file. Commented Jun 25, 2022 at 11:03
  • You may find documentation saying how to remove index.php from codeigniter URL. You need to add .htaccess file with some configs available on codeigniter website and remove index.php inf config file perhaps. Commented Jun 25, 2022 at 11:05
  • you may follow this tutorial programmingfields.com/… Commented Jun 25, 2022 at 11:07
  • Or maybe I'm wrong for codeigniter 4. Commented Jun 25, 2022 at 11:14

2 Answers 2

0

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";
}
Sign up to request clarification or add additional context in comments.

2 Comments

can this be done auto or should I enter a new line of code each time i create a controller it seems a bit weird.
I think i found it first i had to set setAutoRoute(true) in Routes.php then whenever i create a new controller a controller method that will be executed by Auto Routing (Improved) needs HTTP verb (get, post, put, etc.) prefix like getIndex(), postCreate().
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.