John Davidson

php - Codeigniter 4 does not load CSS

0 comments
Message:


CSS is not loaded in Codeigniter 4. My css file is located under /public/assets/css/app.css


I include it like this:


<link rel="stylesheet" href="<?=base_url();?>/assets/css/app.css"/>

I have configured my .env file like this:


app.baseURL = 'http://localhost/[project_name]/'

.htaccess (default Codeigniter file)


DirectoryIndex /public/index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L,QSA]

The browser tries to load the file http://localhost/[project_name]/assets/css/app.css but it fails to load.


What am I doing wrong?



My Answer:
In Codeigniter 4, you need to make sure that you are using the correct base URL for loading CSS files. Here is how you can include your CSS file in your view file:

1. Make sure that your CSS file is located in the correct directory. In this case, it should be located in `/public/assets/css/app.css`.

2. In your view file, you can include the CSS file using the `base_url()` function provided by Codeigniter. Here is an example:

html



3. Make sure that you have set the base URL in your `app/Config/App.php` file. You can set it like this:

php
public $baseURL = 'http://localhost:8080/';


4. Make sure that your `.htaccess` file in the `public` directory is correctly configured to allow access to CSS files. Here is an example of how it should look:



RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]



By following these steps, your CSS file should be loaded correctly in Codeigniter 4.

Rate this post

4 of 5 based on 6338 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us