12/08/2018, 15:28

Laravel 5.5 sẽ có gì mới - Phần 3

Phần 1: https://viblo.asia/p/laravel-55-se-co-gi-moi-Az45bN6N5xY Phần 2: https://viblo.asia/p/laravel-55-se-co-gi-moi-phan-2-6J3ZgD9xlmB 11. Custom Validation Rules Defining The Rule Trong ứng dụng của mình, Taylor Otwell muốn xác nhận 1 Github repository và branch có thực sự tồn tại. Tất ...

Phần 1: https://viblo.asia/p/laravel-55-se-co-gi-moi-Az45bN6N5xY Phần 2: https://viblo.asia/p/laravel-55-se-co-gi-moi-phan-2-6J3ZgD9xlmB

11. Custom Validation Rules

Defining The Rule

Trong ứng dụng của mình, Taylor Otwell muốn xác nhận 1 Github repository và branch có thực sự tồn tại. Tất nhiên để làm được điều này chỉ có cách gọi API tới Github. Để bắt đầu, Taylor định nghĩa 1 class với 2 method passes và message được đặt trong namespace AppRules:

<?php
namespace AppRules;
use AppSource;
use IlluminateContractsValidationRule;
class ValidRepository implements Rule
{
    /**
     * The source control provider instance.
     *
     * @var AppSource
     */
    public $source;
    /**
     * The branch name.
     *
     * @var string
     */
    public $branch;
    /**
     * Create a new rule instance.
     *
     * @param  AppSource  $source
     * @param  string  $branch
     * @return void
     */
    public function __construct($source, $branch)
    {
        $this->source = $source;
        $this->branch = $branch;
    }
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (! $this->source instanceof Source) {
            return false;
        }
        return $this->source->client()->validRepository(
            $value, $this->branch
        );
    }
    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The given repository is invalid.';
    }
}

Xem nào, method passes sẽ nhận 2 đối số $attribute and $value từ Laravel Validator. $attribute là tên trường cần được validate và $value là giá trị của trường đó. Method này sẽ trả về true hoặc false, tương đương với giá trị nhận được có đúng hay không. Trong ứng dụng này, object Source là 1 Eloquent model đại diện cho 1 source control provider ví như GitHub. Method message sẽ trả về thông điệp báo lỗi phù hợp khi method passes trả về false.

Using The Rule

Một khi đã được định nghĩa, bạn có thể sử dụng những validation rule tùy chỉnh trong request. Trong ví dụ này, chúng ta sử dụng method validate, 1 method có sẵn trong object Request trong Laravel 5.5:

<?php
use AppRulesValidRepository;
$request->validate([
    'repository' => [
        'required', 
        new ValidRepository($this->source(), $request->branch)
    ]
]);

Tất nhiên bạn cũng có thể sử dụng chúng trong Form Request hoặc bất cứ đâu mà bạn đã từng sử dụng trước đây.

12. Whoops is coming back

Whoops là 1 PHP framework xử lý lỗi đã từng được cài đặt sẵn trong Laravel 4 và sau đó đã được gỡ bỏ trong phiên bản Laravel 5.0. Và có thông tin rằng Whoops sẽ được mang trở lại trên Laravel 5.5.

Một số tính năng của Whoops:

  1. Flexible, stack-based error handling
  2. Stand-alone library with (currently) no required dependencies
  3. Simple API for dealing with exceptions, trace frames & their data
  4. Includes a pretty rad error page for your webapp projects
  5. Includes the ability to open referenced files directly in your editor and IDE
  6. Includes handlers for different response formats (JSON, XML, SOAP)
  7. Easy to extend and integrate with existing libraries
  8. Clean, well-structured & tested code-base Tin chắc nhiều người sẽ thích sự xuất hiện của các trang lỗi và nhìn thấy đoạn code gây ra lỗi ngay trên trang.

13. Package Auto-Discovery In Laravel 5.5

Trước đây (à hiện tại nữa             </div>
            
            <div class=

0