Search code examples
javascriptphpcsscodeigniter-4

jss and css does not work right after "/"


im currently new at CI4 but i've been working with php for a while. so i've been trying to use codeigniter 4 while watching tutorials but i hit a problem where the css and js wont work right after localhost:8080/something/ at first when i used localhost:8080/something it worked. but when i tried localhost:8080/something/somethingelse, things doesnt work out. weirdly enough. when i used <?= base_url() ?> it still appeared like this

enter image description here

but when i erased <?= base_url()?> it appeared just fine.

enter image description here

but still it only worked for localhost:8080/something and doesn't work for localhost:8080/something/somethingelse. i'm pretty sure the problem is on routing but i dont know how to fix it. any helps would be appreciated

here's my codes

     //.env
    app.baseURL = 'http://localhost:8080/'
    
    database.default.hostname = localhost
    database.default.database = kas_db
    database.default.username = root
    database.default.password = 
    database.default.DBDriver = MySQLi
    
    


    //route
    //this works
    $routes->get('/project', 'project::main');
    //this doesn't
    $routes->get('/project/get', 'project::main');
    
//controller

class project extends BaseController
{

    protected $projectModel;
    public function __construct()
    {
        $this->projectModel = new ProjectModel();
    }

    public function main()
    {

        $project = $this->projectModel->findAll();
        $data = [
            'title' => 'test',
            'project' => $project
        ];
        
        echo view('project/main',$data);
        echo base_url();

    }   


 //css

<!-- Font Awesome -->
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Tempusdominus Bbootstrap 4 -->
  <link rel="stylesheet" href="plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- JQVMap -->
  <link rel="stylesheet" href="plugins/jqvmap/jqvmap.min.css">

Solution

  • Open App.php file in app/Config folder. Set value for $baseURL variable as below:

    public $baseURL = 'Your site url;
    

    Than in your view

    <?php echo base_url('assets/css/style.css'); ?>