Fuel PHP 的学期末总结
嗯……上了一门网页课,被课名唬了——Modern Web Development,结果一个学期上完之后没什么 Modern 的,Web Development 做的也就这样……
不过确实碰到一些配置的东西,这里就记录一下,省的以后如果还有需求 希望不会 会踩坑。
主要就是一些 default 的东西,没有修改 config 文件。
Fuel 下载与安装
主要都是使用命令行操作,并且从最后解压的结果来说文件都挺小的,这在我看来大概是……唯一的优点。
官方文档上只提供了使用 oil
的下载方式,学校机房也是 Linux 系统,所以用 oil
问题不大,Windows 上……完全不能用:
具体下载方式如下:
-
install oil
# install the quick installer $ curl -L https://get.fuelphp.com/oil | sh # create a new project $ oil create <project_name>
-
use composer
# create the default installation of the Fuel framework on virtual host root directory. $ cd /where/ever/your/virtualhost/root/is $ git clone git://github.com/fuel/fuel.git . $ composer install --prefer-dist
或者……
直接到 下载列表 下一个 zip 文件,解压后也是一样的。
(这也是我们被要求做的方法)
然后,安装就完成了。
项目结构如下:
/
docs/
fuel/
app/
core/
packages/
public/
.htaccess
assets/
index.php
oil
fuel
就是主要内核存在的地方,public
是访问是打开的 entry point 以及 assets 存放的地点,public
会调用 fuel 的文件去进行对应的操作。比如说 app/
下面的结构也还有一个 MVC/MVP 内置实现可供选择。
如果在 Fuel PHP 中调用 Assets::
去获取 assets,其对应的是 public/assets/
下的内容。
MVC 部分
Controller
这里就简单的说一下用的 3 种 extendible controller。
-
Controller
这就是比较直接的方式,目前使用的方式就是:
class Controller_Home extends Controller{ public function action_home() { return View::forge('home'); } }
假设该 controller 的路径为
fuel/app/classes/controller
,这个 controller 的访问路径就是默认ip/index.php
或直接使用默认ip
进行访问。虽然文件名结尾是php
,不过访问的方式与 HTML 还是相似的。 -
Controller_Template
这个是在存在可以复用的 layout 时使用,如:
-
controllers/home
class Controller_Home extends Controller_Template{ public $template = 'layout'; public function action_home() { $view = View::forge('home'); $this->template->title = 'Title File'; $this->template->content = $view; } }
-
views/layout.php
<!DOCTYPE html> <html lang="en"> <head> <title><?php echo $title; ?></title> </head> <body> <?php echo $content ?> </body> </html>
基本上还是 HTML 种嵌套 PHP 的代码,设定一些关键字然后做 data injection 这种套路。
-
-
Controller_Rest
Restful Controller
class Controller_m1_Color extends Controller_Rest { public function get_list() { return $this->response(array( 'foo' => Input::get('foo'), 'baz' => array( 1, 50, 219 ), 'empty' => null )); } }
该 API 可以在
http://localhost/test/list.json?foo=bar
进行访问。
View
View 中的东西基本上可以算是 HTML,虽然结尾是 php,不过写法就是……HTML 中嵌套 PHP,上面已经举例了。
Model
目前来说,我还没找到能够在 model
文件夹下建立新的文件夹进行管理的方式,所以目前项目中的 Models 全都在一个文件夹下。还好东西比较少,不然管理起来就很麻烦了……
代码大致如下:
<?php
namespace Model;
use \DB;
class ColorModel extends \Model {
public static function add_color($color, $hex) {
DB::insert('colors')->set(array(
'name' => $color,
'hex' => $hex
)) -> execute();
}
public static function get_color() {
return DB::query('SELECT * FROM `colors`', DB::SELECT)->execute()->as_array();
}
public static function delete_color($id) {
return DB::query('DELETE FROM `colors` WHERE id ='.$id) -> execute();
}
}
?>
这方面的 API 我没有查的很仔细,而且直接使用 use \DB
似乎也是比较古早的用法,现在 fuel php 中提供了内置的 ORM,我这里也没有在使用。
而且这种直接使用 sql 方式绝对会有 SQL injection 的问题。
官方文档中有也有提供一些做法:
// find all articles
$entry = Model_Article::find('all');
// find all articles from category 1 order descending by date
$entry = Model_Article::find('all', array(
'where' => array(
array('category_id', 1),
),
'order_by' => array('date' => 'desc'),
));
// find all articles from category 1 or category 2
$entry = Model_Article::find('all', array(
'where' => array(
array('category_id', 1),
'or' => array(
array('category_id', 2),
),
),
));
使用这种比较合适的方法,Fuel PHP 内部会对输入的数据进行清理,从而预防 SQL Injection。