|
楼主 |
发表于 2007-12-27 09:46:01
|
显示全部楼层
一帖居然发不完,再来一贴。。
Here we are just passing an empty page into the view, so we will have a pretty boring blank white page. This actually won't work yet, since we haven't set any pages for the menus that you see in the header file. We will get to that in a bit =)
这里我们只是添加了一个空页面到模板中。所以我们得到了一个确实枯燥无味的白屏页面,但是我们也确实没有往你在头部文件中看到的菜单里设置任何页面[链接]。我们一会就把它加上去=)
We need to set up a pages table in our database, which you can do with the following SQL:
我们要在数据库中设置一个页面的表,通过下面的SQL代码实现:
SQL复制代码
CREATE TABLE `pages` (
`id` mediumint( 9 ) NOT NULL AUTO_INCREMENT ,
`page_name` VARCHAR( 100 ) NOT NULL ,
`title` VARCHAR( 255 ) NOT NULL ,
`content` longtext NOT NULL ,
`menu` tinyint( 1 ) NOT NULL DEFAULT '0',
`filename` VARCHAR( 255 ) NOT NULL ,
`order` mediumint( 9 ) NOT NULL ,
`date` INT( 11 ) NOT NULL ,
`child_of` mediumint( 9 ) NOT NULL DEFAULT '0',
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `filename` ( `filename` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8 PACK_KEYS =0;
INSERT INTO `pages` ( `id` , `page_name` , `title` , `content` , `menu` , `filename` , `order` , `date` , `child_of` )
VALUES (NULL , 'Home', 'Home', 'Welcome', '0', 'index', '1', '0', '0'
);
复制代码
[注]这里可能需要解释一下的是menu和filename,以及为什么后面插入的date值为0。menu表示其是否为菜单,这里只有一级菜单,filename可能是用来表示链接的名字,data就不好理解了。。下次想到了再写。
Next we will set up a model to grab some pages out of our database and feed them into our controller.
接下来我们将设置一个模块从我们的数据库中抓取出页面并把它们输送给控制器。
application/models/page.php
PHP复制代码
<?php
class Page_Model extends Model {
function get_page_id ($page_name)
{
$query = $this->db->from("pages")->where("filename", $page_name)->get();
return (count($query) > 0) ? $query->current()->id : 0;
}
function get_page ($page)
{
if (isset($page["child"]))
{
$child_id = $this->get_page_id($page["parent"]);
$query = $this->db->from("pages")->where(array("filename" => $page["child"], "child_of" => $child_id))->limit("1")->get();
}
else
{
$query = $this->db->from("pages")->where("filename", $page["parent"])->limit("1")->get();
}
/* Check to see if the page exists */
/*检查页面是否存在*/
if (count($query) > 0)
return $query->current();
else
return false;
}
?>
复制代码
[注]以上代码的用途是通过给定的filename找到其$page。
[注2]在文章的接近尾声的地方才提出$page["child"]是怎么来的,我在这卡了半天。还是不要太看重眼前不懂的地方,也许走一段就有答案了呢。
We will also add in some code into our controller constructor to fetch what page was called from the browser. Also, we will add the main view load to the constructor.
我们同样将添加一些代码到我们的控制器构架中以获取浏览器需要访问的页面。
application/controllers/page.php
PHP复制代码
<?php
function __construct ()
{
parent ::__construct ();
$this->load->model("page");
$this->header = array("menu" => $this->page->get_menu(), "title" => "example.com :: Home");
/* Get the page (page or folder/page) */
/* 获得页面的文件夹和页面文件[注]只是伪静态的文件夹和页面文件,并不是实际的文件夹和页面关系*/
$base = ($this->uri->segment(2) == "edit") ? 2 : 0; //[注]如果uri的第二个字段是edit的则从第三个字段开始判断,如果第二个字段不是edit,说明还在浏览样式的页面下,所以从第一个字段开始判断
if ($this->uri->segment((1+$base)) !== false && $this->uri->segment((2+$base)) !== false) // This is a sub page 如果uri后面有两个个字段,说明是次级页面
$this->location = array("parent" => $this->uri->segment((1+$base)), "child" => $this->uri->segment((2+$base)));
else if ($this->uri->segment((1+$base)) !== false && $this->uri->segment((2+$base)) === false) // This is a main page 如果uri后面只有一个字段,说明是主页面
$this->location = array("parent" => $this->uri->segment((1+$base)));
else // This is the home page 如果后面什么都没有,那它就是首页面
$this->location = array("parent" => "index");
$this->layout = $this->load->view("layout");
$this->layout->footer = ($this->input->get("no_header")) ? "" : $this->load->view("footer"); //[注]如果不输出footer的话也不输出footer。
}
?>
复制代码
In this example, our site will support folder/subpage.html layout, and no deeper. You could expand this system to allow for an infinite level of "folders" if you wish. We will also support having the user add a GET parameter to not load the header and footer, useful for loading pages with ajax.
在这个例子中,我们的站点支持 文件夹/文件 的显示, 而且没有更深的目录结构。只要你愿意你可以扩展这个系统至无限级文件夹,我们同样支持了用户通过增加GET参数来禁止家在头部和底部,这样有利于通过ajax加载文件。
Now we will add this support into the index function:
现在我们增加这个支持到index函数中:
PHP复制代码
<?php
function index ()
{
if ($page = $this->page->get_page($this->location))
{
if (!$this->input->get("no_header"))
$this->layout->header = $this->load->view("header", array_merge($this->header, array("title" => "radd-cpa.org :: " . $page->title)));
else
$this->layout->header = "";
$this->layout->content = $this->load->view("page/index", array("page" => $page));
$this->layout->render(TRUE);
}
else
Kohana ::show_404();
}
?>
复制代码
Here we load the page from the model, and also cleverly show a 404 status if the page doesn't exist (the database result would be FALSE).
这里我们通过模块加载了一个页面,而且在页面不能找到(数据库结果为FALSE)时聪明的的展示一个404状态。
I have provided a whole application for you to look at, pick apart or use on your own projects.
我一进提你正在看的供了整个应用,自己截取一部分或者直接整体应用到你自己的项目中吧。
Feel free to send any changes or improvements back to me at jeremy.bush@kohanaphp.com.
想通过jeremy.bush@kohanaphp.com通知我做了改动或者提升就随时发过来吧。
It includes a basic authentication system for administrating pages, a FCKeditor for easy editing of pages, and some basic HTML views to get you going.
它包含了基本的页面管理的验证系统,FCK编辑器和基本的HTML模板就自己弄吧。
Download Tutorial Files
教程中使用的文件下载地址:http://kohanaphp.com/tutorials/download/page_tutorial.zip |
评分
-
查看全部评分
|