承接 luizgabriel/infire 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

luizgabriel/infire

Composer 安装命令:

composer require luizgabriel/infire

包简介

An extension for eloquent class (Laravel)

关键字:

README 文档

README

An extension for the Eloquent class (Laravel)

##Contents

  • [Installation] (#instalation)
  • [Getting Started] (#getting-started)
  • [Validation with Infire] (#validation-with-infire)
  • [Auto Input Model Filling] (#auto-input-model-filling)
  • [Activing or Deactiving Infire Properties] (#activing-or-deactiving-infire-properties)
  • [File Uploading] (#file-uploading)
  • [Using the Uploader Directly] (#using-the-uploader-directly)

##Installation

Add luizgabriel/infire as a requirement to composer.json:

  "require" : {
        "luizgabriel/infire": "1.0.*@dev"
  }

Update your packages with composer update or install with composer install.

Getting Started

To create a new Infire model, simply make your model class derive from the Infire base class. In the next examples we will use the complete namespaced class to make examples cleaner, but you're encouraged to make use of use in all your classes:

use Luizgabriel\Infire\Infire;

class MyModel extends Infire
{
   // variables and methods
}

Note: you can also add an alias to your laravel configurations by adding 'Infire' => 'Luizgabriel\Infire\Infire'

Validation with Infire

Infire models use Laravel's built-in Validator class. Defining validation rules for a model is simple and is typically done in your model class as a static variable:

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 models validate themselves automatically when Infire->save() is called.

$user           = new User;
$user->name     = 'John doe';
$user->email    = 'john@doe.com';
$user->password = 'test';

$success = $user->save(); // returns false if model is invalid

##Auto Input Model Filling

With infire you can auto fill your model attributes from a form input. For example, let's write a single controller method code.

class MyController extends BaseController
{
    public function store()
    {
        $user = new User;
        
        if(!$user->save())
          return Redirect::back()->withErrors($user->errors());
        
        Redirect::route('users.index');
    }
}

It will have the same effect as

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');
    }
}

##Activing or Deactiving Infire Properties You can deactivate the Auto Fill Model by having a public $autoFillAttributes = false;. Infire auto hash your password fields too, you can deactivate this option by having public $autoHashPassword = false;.

##File Uploading Infire can handle all upload stuff by only having a single array of files like

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.
     */
}

##Using the Uploader Directly When you have some special kind of upload which is not suported by the default post-upload, you can use the $model->uploader(). This gives you the current uploader to use (based on your configurations). For example:

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);
    }
  
}

For the Cloudinary Uploader, the $options array can be used to set informations like width, height, crop and others. (Check more at the [Cloudinary Documentation] (http://cloudinary.com/documentation/image_transformations))

统计信息

  • 总下载量: 465
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-26