• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories

#1
I have asked the following question at stackoverflow: https://stackoverflow.com/questions/6176...irectories

I have following Codeigniter 4 project structure. I am having problem with using base_url() and setting app.baseURL in .env file properly. Following two questions are due to this same problem:

Project Structure

wamp-web-root
    ci_4_project
        public
        app
        assets

APP Structure

public
system
app
    Controllers
        First_controller.php
        Subfolder1
            Subfolder2
                Subfolder3
                    Second_controller.php

FIRST QUESTION - controller sub-directories

My First_controller.php
Code:
<a href="<?php echo base_url()."/public/first_controller";?>">Link</a>"
works fine but my Second_controller.php
Code:
<a href="<?php echo base_url()."/public/subfolder1/subfolder2/subfolder3/second_controller";?>">Link</a>"
is not working.

SECOND QUESTION - accessing assets folder

I use following code to include css files in my views:
Code:
<link href="<?php echo base_url();?>/assets/bootstrap-4.4.1/css/bootstrap.css" rel="stylesheet">


In my .env file: if I use app.baseURL = 'http://localhost/ci_4_project/public/' then Codeigniter Debug Toolbar is displayed but css files are not included in views. If I use app.baseURL = 'http://localhost/ci_4_project/' i.e. without appending public then css files are included in views but Codeigniter Debug Toolbar is not displayed. Apparently this is because assets folder is not inside public folder but instead at same level so when I use public in baseURL, assets are not included. My question is: how to define app.baseURL and whether to add public or not in the url.
Reply

#2
Hi @myo,
You have to put your assets folder inside public folder. Don't put public in your URL. Also, in your <link /> tags, I recommend your to put your URI as parameter of base_url() function: 

<link href="<?= base_url('assets/bootstrap-4.4.1/css/bootstrap.css') ?>" rel="stylesheet">
Reply

#3
Hello myo

1. Is a good practice create a virtual host in your apache server, in this virtual host the root of the domain should be public folder of the CI.

Quote:For example: domain.com -> /route_to_ci/public

https://www.ostechnix.com/configure-apac...tu-part-1/

2. In the .env file put app.BaseURL = http(s)://domain.com

3. In your html page:

PHP Code:
<a href="<?= base_url('first_controller)?>">Link</a>
<
a href="<?= base_url('subfolder1/subfolder2/subfolder3/second_controller')">Link</a>"
<link href="
<?= base_url('assets/bootstrap-4.4.1/css/bootstrap.css');?>" rel="stylesheet"> 


4. Put the assets folder into a public directory.

5. Sorry for my english :-D

I hope you find it useful
Reply

#4
Also, you can simply php spark serve in your project's root. Just make sure that php is registrered in your PATH variable.
Reply

#5
(05-13-2020, 12:09 AM)myo Wrote: I have asked the following question at stackoverflow: https://stackoverflow.com/questions/6176...irectories

I have following Codeigniter 4 project structure. I am having problem with using base_url() and setting app.baseURL in .env file properly. Following two questions are due to this same problem:

Project Structure

wamp-web-root
    ci_4_project
        public
        app
        assets

APP Structure

public
system
app
    Controllers
        First_controller.php
        Subfolder1
            Subfolder2
                Subfolder3
                    Second_controller.php

FIRST QUESTION - controller sub-directories

My First_controller.php
Code:
<a href="<?php echo base_url()."/public/first_controller";?>">Link</a>"
works fine but my Second_controller.php
Code:
<a href="<?php echo base_url()."/public/subfolder1/subfolder2/subfolder3/second_controller";?>">Link</a>"
is not working.

SECOND QUESTION - accessing assets folder

I use following code to include css files in my views:
Code:
<link href="<?php echo base_url();?>/assets/bootstrap-4.4.1/css/bootstrap.css" rel="stylesheet">


In my .env file: if I use app.baseURL = 'http://localhost/ci_4_project/public/' then Codeigniter Debug Toolbar is displayed but css files are not included in views. If I use app.baseURL = 'http://localhost/ci_4_project/' i.e. without appending public then css files are included in views but Codeigniter Debug Toolbar is not displayed. Apparently this is because assets folder is not inside public folder but instead at same level so when I use public in baseURL, assets are not included. My question is: how to define app.baseURL and whether to add public or not in the url.

For your second controller, make sure you correctly namespaced it:

<?php

namespace App\Controllers\Subfolder1\Subfolder2\Subfolder3;

use App\Controllers\BaseController;

class Secondcontroller extends BaseController{
        // Your controller logic here
}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.