Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories (/showthread.php?tid=76445) |
Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - myo - 05-13-2020 I have asked the following question at stackoverflow: https://stackoverflow.com/questions/61768076/codeigniter-4-setting-app-baseurl-in-env-file-controller-sub-directories 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>" Code: <a href="<?php echo base_url()."/public/subfolder1/subfolder2/subfolder3/second_controller";?>">Link</a>" 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. RE: Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - Crosswell - 05-14-2020 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"> RE: Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - hsalardi - 05-14-2020 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-apache-virtual-hosts-ubuntu-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> 4. Put the assets folder into a public directory. 5. Sorry for my english :-D I hope you find it useful RE: Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - Crosswell - 05-16-2020 Also, you can simply php spark serve in your project's root. Just make sure that php is registrered in your PATH variable. RE: Codeigniter 4 - setting app.baseURL in .env file & controller sub-directories - Crosswell - 05-16-2020 (05-13-2020, 12:09 AM)myo Wrote: I have asked the following question at stackoverflow: https://stackoverflow.com/questions/61768076/codeigniter-4-setting-app-baseurl-in-env-file-controller-sub-directories 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 } |