php打造屬於自己的MVC架構

來源:互聯網
上載者:User

一、檔案結構
建立3個檔案夾
controller檔案夾存放控制器檔案
view檔案夾存放視圖檔案
model檔案夾存放資料檔案
建立1個index.php 作為唯一入口
二、控制器
我們在controller檔案夾下建立一個democontroller.php檔案,檔案內容如下 複製代碼 代碼如下:<?php
class DemoController
{
function index()
{
echo('hello world');
}
}
/* End of file democontroller.php */

這個檔案裡面我們只是建立了一個名為DemoController的對象並包含一個index的方法,該方法輸出hello world。下面在index.php中執行DemoController中index方法。
index.php的代碼如下 複製代碼 代碼如下:<?php
require('controller/democontroller.php');
$controller=new DemoController();
$controller->index();
/* End of file index.php */

運行index.php,ok如願我們看到了我們久違的hello world。這兩個檔案非常簡單,但也揭示了一點點mvc的本質,通過唯一入口運行我們要啟動並執行控制器。當然controller部分應該是由uri來決定的,那麼我們來改寫一下index.php使他能通過uri來決定運行那個controller。
index.php改寫代碼如下: 複製代碼 代碼如下:<?php
$c_str=$_GET['c'];
//擷取要啟動並執行controller
$c_name=$c_str.'Controller';
//按照約定url中擷取的controller名字不包含Controller,此處補齊。
$c_path='controller/'.$c_name.'.php';
//按照約定controller檔案要建立在controller檔案夾下,類名要與檔案名稱相同,且檔案名稱要全部小寫。
$method=$_GET['a'];
//擷取要啟動並執行action
require($c_path);
//載入controller檔案
$controller=new $c_name;
//執行個體化controller檔案
$controller->$method();
//運行該執行個體下的action
/* End of file index.php */

在瀏覽器中輸入http://localhost/index.php?c=demo&a=index,得到了我們的hello world。當然如果我們有其他的controller並且要運行它,只要修改url參數中的c和a的值就可以了。
這裡有幾個問題要說明一下。
一、php是動態語言,我們直接可以通過字串new出我們想要的對象和運行我們想要的方法,即上面的new $c_name,我們可以理解成new 'DemoController',因為$c_name本身的值就是'DemoController',當然直接new 'DemoController'這麼寫是不行的,其中的'DemoController'字串必須通過一個變數來中轉一下。方法也是一樣的。
二、我們在url中c的值是demo,也就是說$c_name 的值應該是demoController呀,php不是區分大小寫嗎,這樣也能運行嗎?php區分大小寫這句話不完整,在php中只有變數(前面帶$的)和常量(define定義的)是區分大小寫,而類名方,法名甚至一些關鍵字都是不區分大小寫。而true,false,null等只能全部大寫或全部小寫。當然我們最好在實際編碼過程中區分大小寫。
三、視圖
我們在前面的controller中只是輸出了一個“hello world”,並沒有達到mvc的效果,下面我將在此基礎上增加視圖功能,相信到這裡大家基本已經能想到如何添加視圖功能了。對,就是通過萬惡的require或者include來實現。
首先我們在view檔案夾下建立一個index.php,隨便寫點什麼(呵呵,我寫的還是hello world)。之後我們改寫一下我們之前的DemoController。代碼如下: 複製代碼 代碼如下:<?php
class DemoController
{
function index()
{
require('view/index.php');
}
}
/* End of file democontroller.php */

再在瀏覽器中運行一下,看看是不是已經輸出了我們想要的內容了。
接著我們通過controller向view傳遞一些資料看看,代碼如下: 複製代碼 代碼如下:<?php
class DemoController
{
function index()
{
$data['title']='First Title';
$data['list']=array('A','B','C','D');
require('view/index.php');
}
}
/* End of file democontroller.php */

view檔案夾下index.php檔案代碼如下: 複製代碼 代碼如下:<html>
<head>
<title>demo</title>
</head>
<body>
<h1><?php echo $data['title'];?></h1>
<?php
foreach ($data['list'] as $item)
{
echo $item.'<br>';
}
?>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.