本文摘要
聽說chatGPT很火,還會寫程式碼,而且寫出來的程式碼還真能跑起來!於是我嘗試讓chatGPT給我用PHP寫一個簡單的MVC框架出來。
沒想到寫出來的框架確實挺簡單的,但是又沒覺得哪裡不對,於是我嘗試把這個框架放到伺服器試試能不能跑起來,最後還真的可以跑起來,為了讓大家能夠看到這個框架的演示,我直接爬一個熱搜,然後便於展示資料。
當然了,這個框架只是告訴你框架的基本結構,實際上一個PHP框架的設計是非常精緻的,本文主要是學習框架的基本結構。
框架目錄
框架是真的很簡單,簡單到一眼看完結構。
app/controllers/controller.php
<?php
// 載入模型和檢視
require_once('app/models/model.php');
require_once('app/views/view.php');
class Controller {
private $model;
private $view;
public function __construct() {
// 例項化模型
$this->model = new Model();
// 例項化檢視
$this->view = new View();
}
public function handleRequest() {
// 獲取資料
$data = $this->model->getData();
// 將資料傳遞給檢視
$this->view->render($data);
}
}
?>
app/models/model.php
<?php
class Model {
public function getData() {
// 獲取百度熱搜
$htmlcontent = file_get_contents('https://top.baidu.com/board?tab=realtime');
// 擷取熱搜列表
$hotList = substr($htmlcontent, strripos($htmlcontent, "hotList") + 19);
// 返回列表
return substr($hotList, 0, strrpos($hotList, "moreAppUrl") - 11);
}
}
?>
app/views/view.php
<!DOCTYPE html>
<html>
<head>
<title>爬取百度熱搜</title>
<meta name="wechat-enable-text-zoom-em" content="true">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="color-scheme" content="light dark">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="referrer" content="origin-when-cross-origin">
<meta name="referrer" content="strict-origin-when-cross-origin">
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
body{
background: #EB4226;
}
@media screen and (min-width: 640px) and (max-width: 2000px) {
#logo{
width: 500px;
margin: 100px auto 0;
}
#logo img{
width: 300px;
display: block;
margin: 0 auto;
}
#hotlistCard{
width: 35%;
margin: 30px auto;
background: #fff;
padding: 20px 20px;
border-radius: 20px;
box-shadow: 0 0 10px #ccc;
}
#hotlistCard ul li{
width: 100%;
height: 35px;
font-size: 15px;
line-height: 35px;
}
#hotlistCard ul li a{
text-decoration: none;
color: #333;
}
#hotlistCard ul li a:hover{
color: #5170F2;
text-decoration: underline;
}
#hotlistCard ul li .xh{
width: 25px;
height: 25px;
display: block;
float: left;
line-height: 25px;
text-align: center;
border-radius: 100%;
margin:5px 0 0 0;
margin-right: 8px;
}
#hotlistCard ul li .one_xh{
color: #fff;
background: #FE2D46;
}
#hotlistCard ul li .two_xh{
color: #fff;
background: #F60;
}
#hotlistCard ul li .three_xh{
color: #fff;
background: #FAA90E;
}
#hotlistCard ul li .default_xh{
color: #666;
background: #eee;
}
#hotlistCard ul li .hotScore{
float: right;
color: #999;
font-size: 14px;
line-height: 35px;
}
}
@media screen and (max-width: 639px) {
#logo{
width: 100%;
margin: 30px auto 0;
}
#logo img{
width: 200px;
display: block;
margin: 0 auto;
}
#hotlistCard{
width: calc(90% - 40px);
margin: 30px auto;
background: #fff;
padding: 20px 20px;
border-radius: 20px;
box-shadow: 0 0 10px #ccc;
}
#hotlistCard ul li{
width: 100%;
height: 35px;
font-size: 15px;
line-height: 35px;
}
#hotlistCard ul li .title{
width: 250px;
display: block;
float: left;
white-space: nowrap; text-overflow: ellipsis; overflow: hidden; word-break: break-all;
}
#hotlistCard ul li a{
text-decoration: none;
color: #333;
}
#hotlistCard ul li a:hover{
color: #5170F2;
text-decoration: underline;
}
#hotlistCard ul li .xh{
width: 25px;
height: 25px;
display: block;
float: left;
line-height: 25px;
text-align: center;
border-radius: 100%;
margin:5px 0 0 0;
margin-right: 8px;
}
#hotlistCard ul li .one_xh{
color: #fff;
background: #FE2D46;
}
#hotlistCard ul li .two_xh{
color: #fff;
background: #F60;
}
#hotlistCard ul li .three_xh{
color: #fff;
background: #FAA90E;
}
#hotlistCard ul li .default_xh{
color: #666;
background: #eee;
}
#hotlistCard ul li .hotScore{
float: right;
color: #999;
font-size: 14px;
line-height: 35px;
display: none;
}
}
</style>
</head>
<body>
<!--logo-->
<div id="logo">
<img src="https://fyb-pc-static.cdn.bcebos.com/static/asset/homepage@2x_7926b0bf1e591ee96d2fc68b3a8a1ee0.png" />
</div>
<!--列表-->
<?php
class View {
public function render($data) {
echo '<div id="hotlistCard"><ul>';
$xh = 0;
foreach (json_decode($data,true) as $k => $v) {
// 標題
$baidu_title = json_decode(json_encode($v),true)["query"];
// 連結
$baidu_url = json_decode(json_encode($v),true)["appUrl"];
// 熱度
$baidu_hotScore = json_decode(json_encode($v),true)["hotScore"];
// 序號
$xh = $k+1;
if($k == 0){
echo '<li><span class="xh one_xh">'.$xh.'</span><a href="'.$baidu_url.'" target="blank"><span class="title">'.$baidu_title.'</span></a><span class="hotScore">指數:'.$baidu_hotScore.'</span></li>';
}else if($k == 1){
echo '<li><span class="xh two_xh">'.$xh.'</span><a href="'.$baidu_url.'" target="blank"><span class="title">'.$baidu_title.'</span></a><span class="hotScore">指數:'.$baidu_hotScore.'</span></li>';
}else if($k == 2){
echo '<li><span class="xh three_xh">'.$xh.'</span><a href="'.$baidu_url.'" target="blank"><span class="title">'.$baidu_title.'</span></a><span class="hotScore">指數:'.$baidu_hotScore.'</span></li>';
}else{
echo '<li><span class="xh default_xh">'.$xh.'</span><a href="'.$baidu_url.'" target="blank"><span class="title">'.$baidu_title.'</span></a><span class="hotScore">指數:'.$baidu_hotScore.'</span></li>';
}
}
echo '</ul></div>';
}
}
?>
</body>
</html>
index.php
<?php
// 載入控制器
require('app/controllers/controller.php');
// 例項化控制器
$controller = new Controller();
$controller->handleRequest();
?>
程式碼解釋
MVC框架已經完成了。當使用者請求應用程式時,index.php
將會被呼叫,然後呼叫控制器,並讓控制器處理請求。控制器使用模型來獲取所需的資料,然後使用檢視來呈現資料並返回給使用者。
演示
http://demo.likeyunba.com/php-mvc-framework/
本文作者
TANKING