为什么要用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(); ?>
这里只是第一步,更深入的了解待续。。。。。