October/Winter CMS通过Ajax创建数据

October/Winter CMS通过Ajax创建数据

OctoberCMSAjax框架非常强大易用,本文以Blog插件为例使用ajax框架来创建blog数据

先在layout中引入ajax框架必须的前端文件

{% framework extras %}

pagemarkup中构建表单

<!-- AJAX enabled form -->
<form data-request="onSubmitHandle" data-request-flash>
    <input type="text" name="title" />
    <input type="text" name="slug" />
    <input type="text" name="content" />
    <button type="submit">Submit Fire Ajax</button>
</form>

继续在code中创建业务逻辑

function onSubmitHandle()
{
    $validator = \Validator::make(        
        post(),
        ['title' => 'required']
    );

    if ($validator->fails()) {
        \Flash::error('Title Needed.');
    }
    else {
        $model = new \Rainlab\Blog\Models\Post;
        $model->title = post('title');
        $model->slug = post('slug');
        $model->content = post('content');
        $model->save();    
        \Flash::success('data saved.');
    }

}

怎么样?是不是很简单?

Post Comment