php - Controller behaving erratic. Codeigniter 4 - Stack Overflow

admin2025-04-25  3

The configs for my project

public string $baseURL = '/';

controllers

Home.php

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }
    public function login(): string
    {
        //return view('welcome_message');
        echo "hello";
    }
}
?>

Admin.php

<?php
namespace App\Controllers;

use Codeigniter\Controller;

class Admin extends Controller
{
    public function index(): string
    {
        return view('welcome_message');
    }
}

?>

.htaccess

# Disable directory browsing
Options -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # .html#rewritebase
    # RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Rewrite "www.example -> example"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
ServerSignature Off
# Disable server signature end

routes.php

<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');

When I try

OR

OR

Error: 404 File or directory not found.

Only this url works / OR .php Loads the codeigniter4 welcome page

Similarly also do not work. Gives 404.

Is this something wrong with my setup or something to do with the server. Please advise.

The configs for my project

public string $baseURL = 'http://cyx.vix.co.in/admin_panel/';

controllers

Home.php

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }
    public function login(): string
    {
        //return view('welcome_message');
        echo "hello";
    }
}
?>

Admin.php

<?php
namespace App\Controllers;

use Codeigniter\Controller;

class Admin extends Controller
{
    public function index(): string
    {
        return view('welcome_message');
    }
}

?>

.htaccess

# Disable directory browsing
Options -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    # RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
ServerSignature Off
# Disable server signature end

routes.php

<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');

When I try

http://cyx.vix.co.in/admin_panel/home/login

OR

http://cyx.vix.co.in/admin_panel/home

OR

http://cyx.vix.co.in/admin_panel/home/index

Error: 404 File or directory not found.

Only this url works http://cyx.vix.co.in/admin_panel/ OR http://cyx.vix.co.in/admin_panel/index.php Loads the codeigniter4 welcome page

Similarly http://cyx.vix.co.in/admin_panel/Admin also do not work. Gives 404.

Is this something wrong with my setup or something to do with the server. Please advise.

Share Improve this question edited Feb 11 at 6:02 mickmackusa 48.4k13 gold badges95 silver badges161 bronze badges asked Jan 15 at 13:21 adburyadbury 518 bronze badges 3
  • What have you tried to resolve the problem? Where are you stuck? Does cyx.vix.co.in point to your own server at all? – Nico Haase Commented Jan 15 at 13:52
  • Updates the questions. Pls have a look. – adbury Commented Jan 15 at 13:53
  • Its not the correct url.. Just replace the url with some letters for this forum. But yes. It loads the welcome page for codeigniter correctly if it use just use cyx.vix.co.in/admin_panel – adbury Commented Jan 15 at 14:00
Add a comment  | 

1 Answer 1

Reset to default 1

The following is looking for a file in the "Views" folder:

return view('welcome_message');

in your case it is looking for app/Views/welcome_message.php and this is why you are getting a 404 error.

Also you will get a 404 because your routes are missing the two methods you are trying to call. Add the following to app\Config\Routes.php:

$routes->group('/home', static function ($routes) {
    $routes->match(['GET', 'POST'],'login', 'Home::login');
    $routes->match(['GET', 'POST'],'admin', 'Home::admin');
});

Additionally, please turn on logging so that errors can be displayed. and help you identify where problems are...

You can add the following to your .env file:

CI_ENVIRONMENT = development

You should also uncomment the toolbar in the Filters.php file

转载请注明原文地址:http://anycun.com/QandA/1745578507a90927.html