0

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?

2
  • Did you add a route for this controller function to app/Config/Routes.php? Try adding $routes->get('/home/test', 'Home::test'); Commented Jan 22 at 10:06
  • yes, you are correct, the route works, and I set to use the auto route later. thanks. Commented Jan 22 at 13:25

1 Answer 1

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

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.