luizgabriel / infire
eloquent 类(Laravel)的扩展
此包的规范仓库似乎已不存在,因此该包已被冻结。
Requires
- php: >=5.4.0
- cloudinary/cloudinary_php: 1.0.*
- illuminate/support: 4.2.*
This package is not auto-updated.
Last update: 2019-03-09 00:58:33 UTC
README
eloquent 类(Laravel)的扩展
##内容
- [安装] (#instalation)
- [入门] (#getting-started)
- [使用 Infire 进行验证] (#validation-with-infire)
- [自动填充模型输入] (#auto-input-model-filling)
- [激活或禁用 Infire 属性] (#activing-or-deactiving-infire-properties)
- [文件上传] (#file-uploading)
- [直接使用上传器] (#using-the-uploader-directly)
##安装
将 luizgabriel/infire
添加到 composer.json
的依赖项中
"require" : {
"luizgabriel/infire": "1.0.*@dev"
}
使用 composer update
更新您的包,或使用 composer install
安装。
入门
要创建一个新的 Infire 模型,只需让您的模型类继承自 Infire
基类。在下面的示例中,我们将使用完整命名空间类来使示例更简洁,但您被鼓励在所有类中使用 use
。
use Luizgabriel\Infire\Infire; class MyModel extends Infire { // variables and methods }
注意:您也可以通过添加
'Infire' => 'Luizgabriel\Infire\Infire'
到您的 Laravel 配置中为 Laravel 添加别名。
使用 Infire 进行验证
Infire 模型使用 Laravel 内置的 Validator 类。为模型定义验证规则很简单,通常在模型类中作为静态变量完成。
class User extends Infire { public static $rules = [ 'name' => 'required|between:4,16', 'email' => 'required|email', 'password' => 'required|alpha_num|between:4,8|confirmed', 'password_confirmation' => 'required|alpha_num|between:4,8', ]; }
Infire 模型在调用 Infire->save()
时会自动进行验证。
$user = new User; $user->name = 'John doe'; $user->email = 'john@doe.com'; $user->password = 'test'; $success = $user->save(); // returns false if model is invalid
##自动填充模型输入
使用 Infire,您可以从表单输入自动填充模型属性。例如,让我们编写一个控制器方法的代码。
class MyController extends BaseController { public function store() { $user = new User; if(!$user->save()) return Redirect::back()->withErrors($user->errors()); Redirect::route('users.index'); } }
它将产生与以下相同的效果:
class MyController extends BaseController { public function store() { $user = new User; $user->name = Input::get('name'); $user->email = Input::get('email'); $user->password = Hash::make(Input::get('password')); //Other fields... if(!$user->save()) return Redirect::back()->withErrors($user->errors()); Redirect::route('users.index'); } }
##激活或禁用 Infire 属性您可以通过设置 public $autoFillAttributes = false;
来禁用 自动填充模型。Infire 还会自动哈希密码字段,您可以通过设置 public $autoHashPassword = false;
来禁用此选项。
##文件上传Infire 可以通过只有一个文件数组来处理所有上传操作:
class User extends Infire { protected $fillable = [ 'thumb', 'profile_image', 'name', 'email', 'password', ]; public $files = ['thumb', 'profile_image']; /* * When $user->save() is called. * Your file input is catch and sent to your Cloudinary storage acount. */ }
##直接使用上传器当您有一些默认上传不支持的特殊上传时,您可以使用 $model->uploader()
。这将为您提供一个当前的上传器来使用(基于您的配置)。例如
use Symfony\Component\HttpFoundation\File\UploadedFile as File; class Photo extends Infire { protected $fillable = [ 'image_file', 'user_id' ]; public $files = ['image_file']; public function setImageFileAttribute(File $value) { $upload = $this->uploader()->upload($file); //Sends the file to the storage server and returns informations about it $this->attributes['image_file'] = $this->uploader()->uploadToJson($upload); //Grabs only the important informations } public function getImageFileAttribute($value, $options = array()) { /* * Here you can check if there is no image and return a default one. * It's up to your imagination. */ return $this->uploader()->toUrl($value, $options); } }
对于 Cloudinary 上传器,可以使用 $options
数组设置信息,如宽度、高度、裁剪等。(更多详情请查看 [Cloudinary 文档] (http://cloudinary.com/documentation/image_transformations))