CodeIgniter4 快速入门

Composer安装框架:

composer create-project codeigniter4/framework  项目名

配置数据库

书写路由Router:

普通的添加路由方式:
$routes->add('/', 'Home::index');//localhost
$routes->add('/news/index', 'News::index');//localhost/news/index

//API数据获取【分组式路由】localhost/api/news,localhost/api/news/2,localhost/apic/payfor

$routes->group('api', function($routes)
{
    $routes->get('news/', 'Api\News::index');
    $routes->get('news/(:num)','Api\News::get/$1');
    $routes->add('payfor', 'Api\Pay::index');
});

//   命名空间路由,localhost/admin/news/inde,控制器App\Controllers\Admin\News::index
$routes->add('admin/news/index', 'News::index', ['namespace' => 'App\Controllers\Admin']);
$routes->add('admin/news/create', 'News::create', ['namespace' => 'App\Controllers\Admin']);
$routes->post('admin/news', 'News::save', ['namespace' => 'App\Controllers\Admin']);

控制器


模型

控制器中使用模型:

/**
 * Created by PhpStorm.
 * User: zty
 * Date: 2018/4/23
 * Time: 17:40
 */

namespace App\Controllers;
use App\Models\NewsModel;
use \CodeIgniter\Controller;

class News extends Controller
{
//    默认index方法
    public function index(){
    //模型的使用
        $news = new NewsModel();
        $data = ['news' => $news->findAll(10,0)];
    //视图的使用
        echo view('News/index',$data);
    }
}

视图: application/views文件夹下

以***.php命名,原生PHP语法方式,很容易理解


    
        </span>文章标题<span style="color:#e8bf6a;">
    
    
                foreach ($news as $new) {
        ?>

            

$new['title'];?>

} ?>

Model类数据增删改查:

点击打开链接https://blog.csdn.net/jackyocheung/article/details/80258273

文件上传:

路由:

$routes->post('admin/news', 'News::save', ['namespace' => 'App\Controllers\Admin']);

create文件:application/Views/Admin/News/Create.php

html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">


action="/admin/news" method="post" enctype ="multipart/form-data"> 标题: type="text" name="title">
分类id:type="text" name="catid">
文件:type="file" name="icon">
type="submit" name="提交">
控制器文件:application/Controllers/Admin/News.php

/**
 * Created by PhpStorm.
 * User: zty
 * Date: 2018/5/17
 * Time: 13:35
 */

namespace App\Controllers\Admin;
use CodeIgniter\Controller;

class News extends Controller
{
    public function index(){
        echo view("Admin/News/Index");
    }
    public function create(){


        echo view("Admin/News/Create");
    }

    public function save(){
       $file = $this->request->getFile('icon');
        $file->move(WRITEPATH.'uploads',time().uniqid().'.'.$file->getExtension());
      var_dump($file);
    }
}

Post数据验证

//model中使用验证
class UserModel extends Model
{
        protected $validationRules = [
                'username' => 'required|alpha_numeric_space|min_length[3]',
                'email'    => 'required|valid_email|is_unique[users.email]',
                'password' => 'required|min_length[8]',
                'pass_confirm' => 'required_with[password]|matches[password]'
        ];

        protected $validationMessages = [
                'email' => [
                        'is_unique' => 'Sorry. That email has already been taken. Please choose another.'
                ]
        ];
}

if ($model->save($data) === false)
{
        return view('updateUser', ['errors' => $model->errors()];
}

视图中显示错误信息:


        
$error) : ?>

session

$newdata = array(
        'username'  => 'johndoe',
        'email'     => '[email protected]',
        'logged_in' => TRUE
);
//设置session
$session->set($newdata);

//获取session值
$name = $session->name

//销毁session
$session->destroy();


CSRF验证:

application/Config/Filters.php中设置:

public $globals = [
        'before' => [
                'csrf'
        ]
];
视图页面中使用:


内容是自己实践时,边测试边记得;作为个人笔记参考使用。难免不够详尽。

提供给大家一个手册地址:https://codeigniter-chinese.github.io/codeigniter4-user-guide/intro/index.html点击打开链接


你可能感兴趣的:(技术,产品)