Serve multiple pages from 1 PHP file?

前端 未结 5 1296
失恋的感觉
失恋的感觉 2021-01-19 15:23

So basically i am struggling with the need of optimizing my code for some project of mine. Currently i have pages like add_company.php, view_company.php

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 16:04

    A very easy solution would be to just use includes.

    if ($_GET["action"] == "view") {
      require("action_view.php");
    } else if ...
    

    (of course, you might want to use a switch statement, if you have lots of different page types)

    Then action_view.php contains just the code for the specific page.

    Better, but also more complicated solutions, would be an object oriented approach (abstract Page class with Factory Pattern) or forthright a good framework and template engine.

提交回复
热议问题