学习 fuelphp (3)写一个消息发布 demo

前期准备

fuelphp 框架 

apache + php + mysql 环境

php oil 可以正常使用


开始写代码

1、配置fuel/app/config/config.php

取消 // 'index_file' => flase; 的注释,并修改为  'index_file' => 'index.php' ;//为了隐藏index.php

取消 //'encoding' => 'UTF-8', 的注释,启用 utf-8 编码

取消'alwayes_load' => array()  和里面的 'packages' => array() , 'language' => array()的注释

取消注释并修改后为:

'encoding'  => 'UTF-8',
'index_file' => 'index.php',

'always_load'  => array(

  /**
   * These packages are loaded on Fuel's startup.
   * You can specify them in the following manner:
   *
   * array('auth'); // This will assume the packages are in PKGPATH
   *
   * // Use this format to specify the path to the package explicitly
   * array(
   *     array('auth'    => PKGPATH.'auth/')
   * );
   */
  'packages'  => array(
     'orm',
          'auth',
   ),

  /**
   * These modules are always loaded on Fuel's startup. You can specify them
   * in the following manner:
   *
   * array('module_name');
   *
   * A path must be set in module_paths for this to work.
   */
  // 'modules'  => array(),

  /**
   * Classes to autoload & initialize even when not used
   */
  // 'classes'  => array(),

  /**
   * Configs to autoload
   *
   * Examples: if you want to load 'session' config into a group 'session' you only have to
   * add 'session'. If you want to add it to another group (example: 'auth') you have to
   * add it like 'session' => 'auth'.
   * If you don't want the config in a group use null as groupname.
   */
  // 'config'  => array(),

  /**
   * Language files to autoload
   *
   * Examples: if you want to load 'validation' lang into a group 'validation' you only have to
   * add 'validation'. If you want to add it to another group (example: 'forms') you have to
   * add it like 'validation' => 'forms'.
   * If you don't want the lang in a group use null as groupname.
   */
   'language'  => array(),
),


2、配置fuel/app/config/development/db.php

修改 demo4 为你自己的数据库 , username 和 password 为数据库的连接用户名和密码

'dsn'        => 'mysql:host=localhost;dbname=demo4',
'username'   => 'root',
'password'   => 'root'

3、配置fuel/app/config/routes.php

修改 '_root_' => 'welcome/index', 为如下 '_root_' =>'messages/index'

<?php
return array(
  '_root_'  => 'messages/index',  // The default route
  '_404_'   => 'welcome/404',    // The main 404 route
 
  'hello(/:name)?' => array('welcome/hello', 'name' => 'hello'),
);


4、使用命令行自动生成部分代码,先生成 message 代码

(1)使用命令行进入框架下面存放 oil 的目录下;

(2)输入如下命令: php oil g scaffold messages name:string message:string type:int

该命令解释:

E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1>php oil g scaffold messag
es name:string message:string type:int
PHP Warning:  PHP Startup: Unable to load dynamic library 'E:\Study\Programe\php
Programe\php5\ext\msql.dll' - 找不到指定的模块。
 in Unknown on line 0
        Creating migration: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7
.1\fuel\app\migrations/001_create_messages.php
        Creating model: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\f
uel\app\classes/model/message.php
        Creating controller: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.
7.1\fuel\app\classes/controller/messages.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/messages/index.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/messages/view.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/messages/create.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/messages/edit.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/messages/_form.php
        Creating view: E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1\fu
el\app\views/template.php

生成前后对比图

学习 fuelphp (3)写一个消息发布 demo_第1张图片

(3)在命令窗口再输入命令:php oil r migrate 

E:\Study\Programe\PhpStorm 8.0.1\project\fuelphp-1.7.1>php oil r migrate
PHP Warning:  PHP Startup: Unable to load dynamic library 'E:\Study\Programe\php
Programe\php5\ext\msql.dll' - 找不到指定的模块。
 in Unknown on line 0
Performed migrations for app:default:
001_create_messages

会自动生成 messages 表。

启动 apache 服务器 httpd.exe 后 访问:localhost:8080/index.php/messages/ 

好吧!就到这里。


你可能感兴趣的:(学习 fuelphp (3)写一个消息发布 demo)