Hình mờ php github

Khi đã là một trang web lập trình viên, thì một điều chắc chắn là bạn phải sử dụng lại thư viện của những người khác đã viết để tiết kiệm thời gian và những lỗi phát sinh.

Có rất nhiều thư viện PHP hữu ích được phát triển mỗi ngày, và với sự trợ giúp của Composer và Github, chúng ta có thể dễ dàng khám phá và sử dụng. Dưới đây là một số thư viện thú vị nhất

1. Yêu cầu – Yêu cầu HTTP dễ dàng

Yêu cầu là một thư viện giúp bạn có thể dễ dàng đưa ra các yêu cầu HTTP. Thư viện này bao gồm một số hàm như sau

$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = Requests::get('https://api.github.com/gists', $headers, $options);

var_dump($request->status_code);
// int(200)

var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"

var_dump($request->body);
// string(26891) "[…]"

Với thư viện này, bạn có thể gửi HEAD, GET, POST, PUT, DELETE và các yêu cầu HTTP, thêm các tập tin và các thông số với mảng, và truy cập tất cả các dữ liệu được trả lời

2. Carbon – Thư viện DateTime

Carbon là một API mở rộng đơn giản cho ngày và giờ. Nó giúp tăng cường một số phương pháp hữu ích để làm việc với ngày và thời gian. Ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'

3. Faker – Trình tạo dữ liệu giả

Bạn có thể tạo dữ liệu trực tiếp từ nhiều cách khác nhau. Nhưng với thư viện Faker, bạn có thể tạo ra một bộ dữ liệu giả nhưng không có dữ liệu thật nào khác. Nó tiện dụng khi bạn cần có một bài kiểm tra cơ sở dữ liệu hoặc tạo ra các mẫu dữ liệu cho ứng dụng web của bạn. Nó cũng rất dễ dàng để sử dụng

// require the Faker autoloader
require_once '/path/to/Faker/src/autoload.php';

// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();

// generate data by accessing properties
echo $faker->name; // 'Lucy Cechtelar';

echo $faker->address;
  // "426 Jordy Lodge
  // Cartwrightshire, SC 88120-6700"

echo $faker->text;
  // Sint velit eveniet. Rerum atque repellat voluptatem quia ...

Miễn phí là bạn tiếp tục truy cập vào các thuộc tính của đối tượng, nó sẽ tiếp tục quay trở lại dữ liệu ngẫu nhiên được tạo ra

4. Assetic – Quản lý tài sản

Sử dụng tài sản để quản lý tài sản cho PHP framework. Nó kết hợp và làm gọn CSS/JS của bạn. It is used as after

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;

$js = new AssetCollection(array(
    new GlobAsset('/path/to/js/*'),
    new FileAsset('/path/to/another.js'),
));

// the code is merged when the asset is dumped
echo $js->dump();

Kết hợp tài nguyên theo cách này là một ý tưởng tốt, bởi vì nó có thể tăng tốc độ trang web của bạn. Không chỉ là tổng kích thước giảm mà còn có rất nhiều yêu cầu HTTP không cần thiết bị loại bỏ (hai trong những yếu tố này quyết định đến tốc độ tải trang)

** 5. ImageWorkshop – Thao tác hình ảnh với các lớp **

ImageWorkshop là một thư viện mã nguồn mở cho phép bạn thao tác cấu hình ảnh với các lớp. Với nó, bạn có thể thay đổi kích thước, cắt xén, làm cho hình thu nhỏ. This is a ví dụ

// We initialize the norway layer from the picture norway.jpg
$norwayLayer = ImageWorkshop::initFromPath('/path/to/images/norway.jpg');

// We initialize the watermark layer from the picture watermark.png
$watermarkLayer = ImageWorkshop::initFromPath('/path/to/images/watermark.png');

$image = $norwayLayer->getResult(); // This is the generated image !

header('Content-type: image/jpeg');
imagejpeg($image, null, 95); // We choose to show a JPG with a quality of 95%
exit;

ImageWorkshop được phát triển để thực hiện dễ dàng cho các thao tác với hình ảnh phổ biến nhất trong PHP. Nếu bạn cần một cái gì đó mạnh mẽ hơn nữa, bạn nên xem xét các thư viện Imagine

** 6. Snappy – Ảnh chụp nhanh/Thư viện PDF **

Snappy là một thư viện PHP5 cho phép bạn chụp những bức ảnh hoặc tệp PDF của URL hoặc tài liệu HTML. Nó phụ thuộc vào wkhtmltopdf, có sẵn trên Linux, Windows và OSX. Cách sử dụng nó như sau

require_once '/path/to/snappy/src/autoload.php';

use Knp\Snappy\Pdf;

// Initialize the library with the
// path to the wkhtmltopdf binary:
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

// Display the resulting pdf in the browser
// by setting the Content-type header to pdf:

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');

echo $snappy->getOutput('http://www.github.com');

7. Thành ngữ – Thư viện ORM nhẹ

Idiorm là một thư viện ORM nhẹ và là một truy vấn cho PHP5 được xây dựng dựa trên PDO. Với nó, bạn có thể quên cách viết tà của SQL

$user = ORM::for_table('user')
    ->where_equal('username', 'j4mie')
    ->find_one();

$user->first_name = 'Jamie';
$user->save();

$tweets = ORM::for_table('tweet')
    ->select('tweet.*')
    ->join('user', array(
        'user.id', '=', 'tweet.user_id'
    ))
    ->where_equal('user.username', 'j4mie')
    ->find_many();

foreach ($tweets as $tweet) {
    echo $tweet->text;
}

8. Dấu gạch dưới – Vành đai tiện ích của PHP

Underscore is a port of Underscore. js - Vành đai tiện ích cho các ứng dụng JavaScript. Phiên bản PHP đã không làm cho chúng ta thất vọng mà nó đã hỗ trợ gần như tất cả các chức năng của bản gốc. Một số ví dụ

__::each(array(1, 2, 3), function($num) { echo $num . ','; }); // 1,2,3,

$multiplier = 2;
__::each(array(1, 2, 3), function($num, $index) use ($multiplier) {
  echo $index . '=' . ($num * $multiplier) . ',';
});
// prints: 0=2,1=4,2=6,

__::reduce(array(1, 2, 3), function($memo, $num) { return $memo + $num; }, 0); // 6

__::find(array(1, 2, 3, 4), function($num) { return $num % 2 === 0; }); // 2

__::filter(array(1, 2, 3, 4), function($num) { return $num % 2 === 0; }); // array(2, 4)

Thư viện cũng đã hỗ trợ cho chuỗi, không những vậy nó còn làm cho nó thậm chí còn mạnh mẽ hơn

9. Công văn – Micro Framework

Dispatch là một PHP framework nhỏ. Nó cung cấp cho bạn một thiết lập MVC đầy đủ, nhưng có thể định nghĩa URL quy định và phương pháp tốt hơn ứng dụng của bạn. Hoàn hảo cho các API, các trang web đơn giản hoặc nguyên mẫu

________số 8_______

Nếu kết hợp với một số khung ở đây, bạn có thể có một thiết lập thực sự mạnh mẽ và vô cùng nhẹ

** 10. Buzz – Thư viện yêu cầu HTTP đơn giản**

Buzz is a PHP library for HTTP request. This is a ví dụ

$request = new Buzz\Message\Request('HEAD', '/', 'http://google.com');
$response = new Buzz\Message\Response();

$client = new Buzz\Client\FileGetContents();
$client->send($request, $response);

echo $request;
echo $response;

Nó không có hướng dẫn tài liệu, vì vậy bạn sẽ phải đọc thông tin qua các nguồn mã để có thể biết được tất cả các tùy chọn mà nó hỗ trợ

** 11. Goutte – Thư viện quét web**

Goutte là một thư viện để quét trang web và giải nén dữ liệu

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
0

12. Klein – Bộ định tuyến nhanh như chớp cho PHP

Klein là một thư viện định tuyến (định tuyến) nhẹ dành cho PHP 5. 3 +. Nó có nhiều hơn một số cú pháp, nhưng khá nhanh. This is a ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
1

Bạn cũng có thể đăng ký các phương thức HTTP cụ thể và sử dụng các biểu thức chính quy như đường dẫn

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
2

Điều này rất tốt cho các dự án nhỏ, nhưng bạn phải xử lý các kỹ năng khi sử dụng một thư viện như thế này để các ứng dụng lớn hơn, nhưng mã của bạn có thể bảo trì và nâng cấp rất nhanh. Với mục đích này, bạn sẽ nhận được đầy đủ khung MVC framework như Laravel hoặc CodeIgniter

13. Ubench – Thư viện điểm chuẩn vi mô

Ubench is an small library for your PHP code standard. Nó theo dõi thời gian thực hiện và sử dụng bộ nhớ. This is a ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
3

14. Xác thực – Công cụ xác thực đầu vào

Xác thực là công cụ xác nhận tuyệt vời nhất từng được tạo ra cho PHP. Nhưng nó có thể cung cấp được gì không?

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
4

Với thư viện này, bạn có thể xác nhận các dữ liệu người dùng gửi. Ngoài ra, nó hỗ trợ một số lượng lớn các kiểm tra hiện có, ngoại lệ và thông báo lỗi tùy chỉnh

15. Filterus – Thư viện lọc

Filterus là một thư viện bộ lọc, nhưng nó cũng có thể xác định đầu vào của bộ lọc để phù hợp với một mô hình định sẵn. This is a ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
5

Filterus có rất nhiều mô hình tích hợp, hỗ trợ chuỗi và thậm chí có thể xác nhận các phần tử mảng với quy tắc xác nhận cá nhân

17. Tải lên - Để xử lý tệp tải lên

Tải lên là một thư viện đơn giản hóa tệp tải lên và xác nhận. Khi một mẫu được gửi, thư viện có thể kiểm tra các loại tệp và kích thước

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
6

nó giúp tiết kiệm rất nhiều dòng mã

17. Ham – Thư viện định tuyến với bộ nhớ đệm

Ham cũng là một khuôn khổ định tuyến nhẹ nhưng nó sử dụng bộ nhớ đệm để tăng tốc độ nhiều hơn. Nó đạt được điều này bằng cách cache bất cứ điều gì I/O liên quan trong XCache/APC. This is a ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
7

Thư viện yêu cầu bạn phải cài đặt một trong hai XCache hoặc APC, có nghĩa là nó sẽ không hoạt động trên hầu hết các máy chủ lưu trữ

18. PHP Geo – Thư viện vị trí địa lý

PHPGEO là một thư viện đơn giản để tính toán khoảng cách giữa các tọa độ địa lý với độ chính xác cao. Ví dụ

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRFC2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT');

// comparisons are always done in UTC
if (Carbon::now()->gte($endOfWorld)) {
    die();
}

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
8

Điều này sẽ làm việc tốt trong các ứng dụng có sử dụng vị trí dữ liệu. Để có được chính xác, bạn có thể sử dụng HTML5 từ API, API của Yahoo (hoặc cả hai, như chúng tôi đã làm trong ứng dụng web thời tiết hướng dẫn một>)