redeman/imgur-laravel
Composer 安装命令:
composer require redeman/imgur-laravel
包简介
Laravel Package for Imgur API. Provides easy access to the adyg/php-imgur-api-client library.
README 文档
README
A Laravel (4 / 5) Package for using the Imgur api. Internally we use adyg/php-imgur-api-client. The package provides a service provider, some configuration and a facade, such that you should be able to get started with writing your app immediately.
For more detailed documentation on how to use adyg/php-imgur-api-client you should look at their documentation.
Quick example
use Redeman\Imgur\Facades\Imgur; $images = Imgur::api('gallery')->randomGalleryImages(); foreach ($images as $image) { echo '<li><img src="' . $image->getLink() . '"></li>'; }
Getting started
First you will have to install the package using composer, this can be done by adding the following to your require block,
"redeman/imgur-laravel": "~1.0",
Next you can register the service provider by adding,
'Redeman\Imgur\ImgurServiceProvider',
to your providers array (in config/app.php).
Imgur uses Oauth 2.0 to authenticate users. Therefore you have get a authentication token from your users if you want them to be able to view and upload images.
In Laravel 5 you can do this by first add a route middleware to the Redeman\Imgur\Middleware\AuthenticateImgur middleware in your App\Http\Kernel.
/** * The application's route middleware. * * @var array */ protected $routeMiddleware = [ // your other route middleware 'imgur' => 'Redeman\Imgur\Middleware\AuthenticateImgur', ];
Next you should add the imgur middleware to any route were a user should be authenticated by Imgur.
The AuthenticateImgur middleware will store and retrieve the user's access token.
If the user is not authenticated by the Imgur (meaning your application doesn't know the user's access token), then the user will be redirected to a route with the name imgur.authenticate.
If you are a Laravel 4 user, then you only need to add the service provider. This provider will register a imgur filter which you can use for your routes, see the example below how to do this.
In the following section we show an example of some simple routes.
Example
routes.php:
// Show the user a random image Route::get('/', ['middleware' => ['imgur'], function() { $client = App::make('Imgur\Client'); $images = $client->api('gallery')->randomGalleryImages(); return View::make('imgur.images')->with('images', $images); }]); // Ask the user to authenticate using Imgur's services Route::get('imgur/authenticate', ['as' => 'imgur.authenticate', function() { $client = App::make('Imgur\Client'); return View::make('imgur.authenticate')->with('imgurUrl', $client->getAuthenticationUrl()); }]);
If you are a Laravel 4 user, then you should substitute middleware with before.
imgur.images.blade.php:
<p>Here are some images for you to enjoy:</p> @foreach ($images as $image) <img src="{{ $image->getLink() }}"> @endforeach
imgur.authenticate.blade.php:
<h2>You need to register with Imgur in order to visit this page:</h2> <a href="{{ $imgurUrl }}">Click to authorize</a>
Now you're all set! The next section describes how you can configure the package to use your client id, secret and a custom TokenStorage.
Configuration
First publish the configuration file:
Laravel 4:
php artisan config:publish redeman/imgur-laravel
Laravel 5:
php artisan vendor:publish --provider="Redeman\Imgur\ImgurServiceProviderLaravel5"
Now you can fill in the appropriate values for client_id and client_secret. It is encouraged to use a .env which contains your client id and secret. Here is how you can do this in laravel 5:
/* * Public client id */ 'client_id' => env('CLIENT_ID'), /** * Client secret */ 'client_secret' => env('CLIENT_SECRET'),
Read the Laravel documentation to learn more about environment configuration.
Using a custom TokenStorage
The AuthenticateImgur middleware uses a Redeman\Imgur\TokenStorage\Storage interface to store a user's access token. By default we provide a SessionStorage which will save the data in the user's session.
However you could also use a database or some other storage facility. To do this you will have to change the token_storage value in your config/imgur.php file to the name of the class that you want to use.
Todos:
- More storage facilities by default
- Add tests for the middleware and filters
redeman/imgur-laravel 适用场景与选型建议
redeman/imgur-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.64k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「wrapper」 「laravel」 「imgur」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 redeman/imgur-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 redeman/imgur-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 redeman/imgur-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A PSR-7 compatible library for making CRUD API endpoints
gv config package
A PHP (read-only) wrapper for the Git command line utility
Alfabank REST API integration
统计信息
- 总下载量: 2.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-02