codeignitercodeigniter-4

Controller behaving erratic. Codeigniter 4


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.


Solution

  • 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