Content Index
We have several ways to work with CodeIgniter 4, the most common is to run it directly from the Apache server, once the organization of files and folders in CodeIgniter 4 is known:
We have several ways to work with CodeIgniter 4, the most common is to run it directly from the Apache server:
Laragon / Laravel Herd
If you use Windows and Laragon or Laravel Herd on MacOS or Windows, you will have noticed that in both cases it generates a clean URL through virtual hosts automatically to access the application:
<VirtualHost *:80>
DocumentRoot "C:/laragon/www/peliculas/public"
ServerName peliculas.test
ServerAlias *.peliculas.test
<Directory "C:/laragon/www/peliculas/public">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>How to access the clean URL?
This is a virtual host that both Laragon and Laravel Herd auto-generate for us, and being auto-generated, they are managed internally, but you can see which ones you have created from the Laragon application in:
Menu - Apache - sites enabledOr Laravel Herd from the Sites option.
Why is this necessary? Very easy: every time Laragon or Laravel Herd starts, it performs a scan of your folders. If it detects a new project, it automatically creates a clean URL using a Virtual Host.
Of course, if you use Apache on another operating system like Linux or MacOS, or simply use another server on Windows, you can create your own virtual host.
Using the Terminal and PHP Spark
Another option, which is the one we are going to use as it is the most direct, is to directly use the spark server that we have in CodeIgniter 4 when installing the framework:
$ php spark serveThis command must be executed from the root of the project; that is, if our project is named "peliculas", then we must place ourselves in our terminal at:
C:\laragon\www\peliculas
Important to note that if you use spark, Apache is not necessary, and for the purposes of following this book, you will only need PHP and MySQL.
Also remember to restart your server so that Laragon detects the new project and creates the virtual host.
The key change: The public folder
A fundamental difference between CodeIgniter 3 and CodeIgniter 4 is the location of the index.php file (the framework's boot file).
- In version 3, it was in the root.
- In version 4, it is located inside the /public folder.
For security reasons, the web server must point to the /public folder. If you access manually via localhost/project/public, the project will work, but I do not recommend this scenario. URL management becomes quite complicated and you could have problems as we progress in development.
From localhost, if you use Apache
The last one would be to access the entire path from Apache:
http://localhost/peliculas/public/
Which is not very recommended, as it can bring complications with handling routes and referencing the base URL, and it is a bit tedious to work with those types of URLs.
Optional, if you use Apache
You can create your virtual host in the httpd.conf file; for more information, you can see this link.
Project Access Summary
In short, you have three ways to view your work, listed from most to least recommended:
- Through Virtual Host (Virtual URL): Example http://project.test. It is the best way for professional development.
- Through PHP Spark: Running php spark serve for a fast development server.
Next step, learn how you can view errors in the browser in CodeIgniter 4.