how to implement mvc in core php

前端 未结 8 2202
余生分开走
余生分开走 2020-12-29 00:52

how is mvc architecture used in php without any framework?

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 01:22

    By writing your own MVC framework that fallows MVC pattern and OOP principles :)

    1. You need to have Front Controller so every HTTP Request goes through one file, index.php, app.php or what ever you want. This way you can configure application in one place.

    2. From there you need Routing mechanism that will analyze HTTP Request, current URL, HTTP Header verb / method, and based on that you will invoke appropriate Controller Method / Action Controller.

    3. From Controller, you can access your Models that will deal with "heavy lifting", deal with database and domain / business logic etc. And from Controller you can render Views.

    So you need at least Front Controller, Router / Dispatcher, Controller, Models and Views to have simple MVC arhitecture.

    You would do that joust as other MVC web frameworks do, with slight variations depending on your preferences.

    Take a look at some simple frameworks like Codeigniter, and read their source code to get idea how they are doing MVC.

    And have fun building your MVC! Its all about the fun after all :D

提交回复
热议问题