CodeIgniter框架了解-1
Cemon_Liu Lv4

从案例中学习框架

  1. 网页访问需要设置url 对应的Routers 位于 app/Config/Routes.php
  2. 通过routes的指向到对应的控制器类(需要引用对应的类,不然无法识别)
  3. 在控制前内部写对应的方法,默认index()
  4. 在方法内部return view(‘page’),这里page是对应的视图page
  5. 在Views里面建立page.php 用于前段展示
  6. return view()方法可以串联不同的page进行展示
  7. 数据库配置env文件应该修改文件名加点,否则配置文件不生效

建立数据库和模型

  1. 使用Migration和Seeder是可以初始化table并且填入一些sample数据的
  • php spark make:migration Create_User_table 可以创建一个migrate文件,然后使用$this->forge()命令去创建相应的数据和表。 php spark migrate去执行这个文件、
  • php spark make:model User命令创建一个UserModel
  • php spark make:seeder UserSeeder 可以创建一个seeder文件。在文件里创建默认的数据。php spark db:seed UserSeeder执行将seeder中的数据放入数据库中

Migrate文件的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddBlog extends Migration
{
public function up()
{
$this->forge->addField([
'blog_id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true,
],
'blog_title' => [
'type' => 'VARCHAR',
'constraint' => '100',
],
'blog_description' => [
'type' => 'TEXT',
'null' => true,
],
]);
$this->forge->addKey('blog_id', true);
$this->forge->createTable('blog');
}
public function down()
{
$this->forge->dropTable('blog');
}
}

Seeder的文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class SimpleSeeder extends Seeder
{
public function run()
{
$data = [
'username' => 'darth',
'email' => 'darth@theempire.com',
];

// Simple Queries
$this->db->query('INSERT INTO users (username, email) VALUES(:username:, :email:)', $data);

// Using Query Builder
$this->db->table('users')->insert($data);
}
}
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量