MVC

Custom MVC framework Logic

There are 5 pillars of any MVC framework.
1) Router
2) Validator
3) Database
4) Template Engine
5) Container

Framework is foundation. Though there are very popular frameworks like Symphony & Laravel that we can use. But our goal is to learn the MVC structure not framework. If we learn that, we can easily use any framework.

We are following this framework: https://github.com/ZTMLuisRamirez/phpiggy

There is a proper guide from the zero to last on Udemy about how to build this. But we are exploring some basics/ideas of core files;

Composer – We use composer because we use many libraries in our application and libraries are always depends on another libraries. So We want a automatic way by which when we use a library A, it automatically import the dependent libraries without any manual inputs. That’s why composer is there in PHP. It downloads the library list into your application directory and creates a composer.json file.

BootstrapCreates the application instance

RouterAccepts URI and normalize the URI and decide where to route the user

In router class, do normalization of URL as we would like to see:
about/user -> /about/user/
/about/user -> /about/user/
/about/user/ -> /about/user/ using regular expression function.

Controllers – Manages all request handling all logic of a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users.

Namespaces – Are generally used to reuse the class name. For example User class can be define in namespace A and in namespace B. When we want to use namespace A -> user class, Just use namespace A before writing code. That’s it. Else use namespace B if you want that user class from B.
It avoid naming conflicts, Organize code & Namespaces can shorten long names, which improves the readability of source code.

Container – is responsible to to manage class dependencies and performing dependency injections.

Now what is dependency Injection?
Suppose we are in a class B and we are dependent on another class A. So Dependency is : we are injecting an object of the class A in a constructor of class B.

Check this tutorial for easy idea: https://ramkumarkhub.medium.com/dependency-injection-in-javascript-cbe5e6d76d1a

The core logic is: we don’t want to create instance of Class A into constructor of class B. It’s better to create instance of class A outside of class B and inject it into constructor of class B. By this way instance of class A also can be used by other classes if required. So we don’t need to create many instances of Class A. Just one is enough to use at all. Creating instance is required memory and we want to save memory so it’s advisable to create minimum instance of class.

Middleware – Provides a mechanism of filtering HTTP requests before entering into your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen.

Services – Services are different classes which can use by many controllers. Ideally controllers are only responsible to get / post request and send a response back. The other activities can be defined in form of services/different classes. So any controller may have # of services. Form validation is a service.

Ask How to? The answer must be logic of Framework.
Ask What/where to? The answer must be logic of Application