首先准备目录,我们将复制contact插件中的contactform组件:
重命名和目录/文件: \plugins\raiseinfo\movies\components\contactform \plugins\raiseinfo\movies\components\ContactForm.php 到: \plugins\raiseinfo\movies\components\actorform \plugins\raiseinfo\movies\components\ActorForm.php
下面修改拷贝过来的文件: \plugins\raiseinfo\movies\components\ActorForm.php
namespace Raiseinfo\Movies\Components; use Cms\Classes\ComponentBase; class ActorForm extends ComponentBase { public function componentDetails() { return [ 'name' => 'Actor Form', 'description' => 'Enter actor form' ]; } public function onSave() { } }
\plugins\raiseinfo\movies\components\actorform\default.htm
<form data-request="onSave"> <label>Name:</label> <input type="text" name="name" class="form-control"> <label>Last name:</label> <input type="text" name="lastname" class="form-control"> <button type="submit" class="btn btn-success form-control">Save</button> </form>
\plugins\raiseinfo\movies\Plugin.php 修改registerComponents方法:
public function registerComponents() { return [ 'Raiseinfo\Movies\Components\Actors' => 'actors', 'Raiseinfo\Movies\Components\ActorForm' => 'actorform' ]; }
测试这个组件是否可用(虽然没有功能):
我们将创建一个新的页面:
我们点击保存,系统没有任何反应,因为我们没有对onSave方法进行定义。
use Illuminate\Support\Facades\Input; use Raiseinfo\Movies\Models\Actor; ... public function onSave() { $actor = new Actor(); $actor->name = Input::get('name'); $actor->lastname = Input::get('lastname'); $actor->save(); }
下面我们试试可否添加演员信息
保存演员信息成功了。 是不是还有些不完美?起码要保存成功需要给用户一个提示吧。 October可以通过Flash实现发送消息。
C:\laragon\www\octobermovie\plugins\raiseinfo\movies\components\ActorForm.php
namespace Raiseinfo\Movies\Components; use Cms\Classes\ComponentBase; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Redirect; use Raiseinfo\Movies\Models\Actor; use Flash; class ActorForm extends ComponentBase { public function componentDetails() { return [ 'name' => 'Actor Form', 'description' => 'Enter actor form' ]; } public function onSave() { $actor = new Actor(); $actor->name = Input::get('name'); $actor->lastname = Input::get('lastname'); $actor->save(); Flash::success('Actor add success'); return Redirect::back(); } }
C:\laragon\www\octobermovie\plugins\raiseinfo\movies\components\actorform\default.htm
<form data-request="onSave"> <label>Name:</label> <input type="text" name="name" class="form-control"> <label>Last name:</label> <input type="text" name="lastname" class="form-control"> <button type="submit" class="btn btn-success form-control">Save</button> {% flash success %} <p>{{ message }}</p> {% endflash %} </form>
添加新演员,提示信息显示正常