PHP MVC 框架Symfony初探(一)

为什么要用MVC,为什么是Symfony,这些不解释,不了解的查资料。

Symfony I JUST UNDERSTAND IT。

首先,介绍一本书:Symfony_book_2.5.pdf,从Symfony官网上下载,里面很详细的介绍了Symfony。

其次:了解Symfony前,先去看看Composer,及 Twig。Composer是个下载工具,Twig是个模板语言(暂时这么说吧)

接着,做个例子,看看Symfony的前世今生。

看Demo效果:

很简单,就一个List,一个详细介绍。

目录结构:

看代码:

//layout.php

<!DOCTYPE html><html><head><title><?php echo $title ?></title></head><body><?php echo $content?></body></html>



 
  
 
<strong>//list.php</strong>
<?php $title = 'List of Posts'?>
<?php ob_start()?>
<h1>List of Posts</h1>
<ul>
<?php foreach ($posts as $post): ?>
<li><a href="/myproject/phpflatblog/index.php/show?id=<?php echo $post['id'] ?>"><%--这里的地址请修改--%>
<?php echo $post['title']?>
</a></li><?php endforeach; ?>
</ul>
<?php $content = ob_get_clean()?>
<?php include 'layout.php' ?>

<strong>//show.php</strong>
<?php $title = $post['title'] ?>
<?php ob_start() ?>
<h1><?php echo $post['title'] ?></h1>
<div class="date"><?php echo $post['date'] ?></div>
<div class="body">
<?php echo $post['body'] ?>
</div>
<?php $content = ob_get_clean() ?>
<?php include 'layout.php' ?>

<?php
<strong>//controllers.php</strong>
use Symfony\Component\HttpFoundation\Response;

function list_action()
{
	$posts = get_all_posts();
	$html=  render_template('templates/list.php',array('posts'=>$posts));
	return new Response($html);
}
function show_action($id)
{
	$post = get_post_by_id($id);
	$html=  render_template('templates/show.php',array('post'=>$post));
	return new Response($html);
}

// helper function to render templates
function render_template($path, array $args)
{
	extract($args);
	ob_start();
	require $path;
	$html = ob_get_clean();
	return $html;
}

?>
<?php
<strong>// model.php</strong>
function open_database_connection() {
	$link = mysql_connect ( 'localhost', 'root', '' );
	mysql_select_db ( 'blog_db', $link );
	return $link;
}
function close_database_connection($link) {
	mysql_close ( $link );
}
function get_all_posts() {
	$link = open_database_connection ();
	$result = mysql_query ( 'SELECT id, title FROM post', $link );
	$posts = array ();
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		$posts [] = $row;
	}
	close_database_connection ( $link );
	return $posts;
}

function get_post_by_id($id)
{
	$link = open_database_connection();
	$id = intval($id);
	$query = 'SELECT date, title, body FROM post WHERE id = '.$id;
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	close_database_connection($link);
	return $row;
}
?>

<?php
<strong>// index.php</strong>
// load and initialize any global libraries
require_once 'model.php';
require_once 'controllers.php';
require_once 'vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();

// route the request internally
//$uri = $_SERVER['PHP_SELF'];
$uri = $request->getPathInfo();

if ('/' == $uri) {
	$response =list_action();
} elseif ('/show' == $uri && $request->query->has('id') ) {
	$response =show_action($request->query->has('id'));
} else {
	header('Status: 404 Not Found');
	$response =new $response( '<html><body><h1>Page Not Found.</h1></body></html>',Response::HTTP_NOT_FOUND);
}

$response->send();
?>

这里只是第一步,更深入的了解待续。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值