php - How to use CodeIgniter 4.6 in subfolder? - Stack Overflow

admin2025-04-22  3

I am trying to create a new site under xampp (php 8.2) with CodeIgniter 4.6(the latest). Here are what I did:

  1. run cmd: composer create-project codeigniter4/appstarter c46 under folder htdocs

  2. update baseURL in App.php like below

    public string $baseURL = 'http://localhost:988/c46/'; // also tried with public at end

Now, I can go to http://localhost:988/c46/public/ to see the default welcome page (the one from Home controller index method). all good so far.

I added a new function in controller Home.php

public function test(): string
{
    return 'ok';
}

Here is my issue, I could not access the page home/test. Seems only the default controller and method is working. I tried:

  • http://localhost:988/c46/public/home/test -- 404 message displayed
  • http://localhost:988/c46/public/index.php?home/test -- this goes to the default page
  • http://localhost:988/c46/public/index.php/home/test -- 404 message displayed

I also tried to change .htaccess file in public folder RewriteBase to below, both not working

RewriteBase /c46
RewriteBase /c46/public

How can I access the new page/method in controller? what are the changes I need to make to get this work?

I am trying to create a new site under xampp (php 8.2) with CodeIgniter 4.6(the latest). Here are what I did:

  1. run cmd: composer create-project codeigniter4/appstarter c46 under folder htdocs

  2. update baseURL in App.php like below

    public string $baseURL = 'http://localhost:988/c46/'; // also tried with public at end

Now, I can go to http://localhost:988/c46/public/ to see the default welcome page (the one from Home controller index method). all good so far.

I added a new function in controller Home.php

public function test(): string
{
    return 'ok';
}

Here is my issue, I could not access the page home/test. Seems only the default controller and method is working. I tried:

  • http://localhost:988/c46/public/home/test -- 404 message displayed
  • http://localhost:988/c46/public/index.php?home/test -- this goes to the default page
  • http://localhost:988/c46/public/index.php/home/test -- 404 message displayed

I also tried to change .htaccess file in public folder RewriteBase to below, both not working

RewriteBase /c46
RewriteBase /c46/public

How can I access the new page/method in controller? what are the changes I need to make to get this work?

Share Improve this question edited Feb 11 at 6:02 mickmackusa 48.3k13 gold badges95 silver badges161 bronze badges Recognized by PHP Collective asked Jan 21 at 22:45 Alan shenAlan shen 216 bronze badges 2
  • Did you add a route for this controller function to app/Config/Routes.php? Try adding $routes->get('/home/test', 'Home::test'); – Marleen Commented Jan 22 at 10:06
  • yes, you are correct, the route works, and I set to use the auto route later. thanks. – Alan shen Commented Jan 22 at 13:25
Add a comment  | 

1 Answer 1

Reset to default 0

I too ran into some problems using the ci4 routing while having the project in a subdirectory, here's what I did:

  1. Change the $baseURL value your app/Config/App.php to http://localhost:988/c46/
  2. Add a .htaccess in the root directory of your project (you'll want to place it in the root of the c46 directory) to rewrite yout http requests from http://project to http://project/public

Here's an example of the .htaccess you can add to your project:

RewriteEngine On

# You might want to enable this, it redirects users from http://url/dir to http://url/dir/
DirectorySlash On

RewriteCond %{REQUEST_URI} !^/c46/public/
RewriteRule .* /c46/public/$0 [L]

DirectoryIndex index.php
转载请注明原文地址:http://anycun.com/QandA/1745283738a90540.html