-1

I am working on my own MVC framework for learning purposes. In other frameworks like CI4, we can pass the controller name and method to the URL and it will go to that method and run it.

for example "http://localhost/myMVC/Home/index" in CI4 if we write this it will go to Home controller and into the index method. We can use the link in href of a tag or simply call it directly it works.

Whenever I search on google it just opens CI4 or laravel documentation or CI4 or Laravel code. What I want is core PHP implementation. I have managed to create a base controller and implement the view method. This one is a bit confusing and I don't know what to search for exactly on google. I have managed to access Home controller by "http://localhost/myMVC/Controller/Home.php" it does get me inside Home controller but I want to do it the way other frame works have done line passing just Controller name and then method to get access the method in Controller classes.

Now I want to know how they did that so I can apply that to my framework.

Github Link to my MVC project click here.

ALI HAIDER
  • 27
  • 5
  • Why not read the source code of CI4 to understand how this works? That's the beauty of open-source software – Nico Haase Dec 05 '21 at 11:00
  • I have been using CI4 for almost a year now, I tried to read and implement it but it was a bit difficult for me. So I wanted to search for something simple which will help me understand core PHP better. You see I never learned core PHP I started directly from CI3 frame work then migrated to 4. So that is why I decided to build something similar to learn clear concepts of core PHP. – ALI HAIDER Dec 05 '21 at 14:46

1 Answers1

1

Notes:

  • I'm using Ubuntu. So, eventual paths below follow Ubuntu's filesystem rules.
  • I'm using Apache/2.4.41 (Ubuntu) web server. So, any settings below follow its rules.

Disallow access to the whole filesystem:

First of all, you need to assure, that, by default, your web server (I suppose it's Apache) doesn't allow access to any resource of the filesystem. So, in the configuration file of Apache (/path/to/apache2/apache2.conf) should appear the following:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

The project root and the document root shouldn't be the same:

Right now, your document root is the same with the project root. Though, they shouldn't be the same.

First, the whole project root should be made inaccessible to public access. Disallowing the access to the whole filesystem by default - as described above - should achieve this.

Second, in the project root you should create a new directory - let's name it public - and use it as the document root. Only this folder of your MVC-based application should be publicly accessible! Therefore, the file index.php should be moved to it. To this directory belong all other publicly accessible assets too: .css files, .js files, images, node_modules, fonts, etc.

Create a virtual host for your application:

In the configuration file of your web server (/path/to/apache2/apache2.conf) you should further create a virtual host, as below. Alternatively, these virtual host settings can be written in a .htaccess file (/path/to/projects/myMVC/.htaccess). The file should be created in the project root.

<VirtualHost *:80>
    # The server name is completed in the address bar of the 
    # browser, in order to access the MVC-based application.
    # The name is your choice.
    ServerName local.my-mvc
    DocumentRoot "/path/to/projects/myMVC/public"
    
    <Directory "/path/to/projects/myMVC/public">
        # Access is allowed unconditionally...
        Require all granted

        # ... or can be limited to a certain ip (here, as example: localhost, e.g. 127.0.0.1)
        # Require ip 127.0.0.1

        # When Options is set to "off", then the RewriteRule directive is forbidden!
        Options FollowSymLinks
        
        # Activate rewriting engine.
        RewriteEngine On
        
        # Use RewriteBase if webserver's URL is not directly related to physical file paths.
        RewriteBase /
        
        # Rewrite the request if the requested path and file doesn't directly match a physical file.
        RewriteCond %{REQUEST_FILENAME} !-d
        
        # Rewrite the request if the requested path and file doesn't directly match a physical folder.
        RewriteCond %{REQUEST_FILENAME} !-f
        
        # Rewrite the request to the index.php script.
        RewriteRule ^(.*)$ index.php [QSA,L]
    </Directory>
</VirtualHost>

Add the server name of the application to the "hosts" file:

At last, the server name of the application must be added to the hosts file (/etc/hosts), as follows. Note that you'll need to have administrator rights in order to save the changes made to this file:

127.0.0.1   local.my-mvc

After you make sure that all of the above works as expected, you will have to edit the file index.php with the following steps:

  • Read and parse the URL, e.g. split it in components.
  • Find the controller class corresponding to the controller component of the URL.
  • In the found controller class find the controller method corresponding to the method component of the URL.
  • Call the method.

Recommendations:

Good luck!

Resources:

About MVC-based applications:

About Apache settings:

PajuranCodes
  • 303
  • 3
  • 12
  • 43