1. Go to this page and download the library: Download givebutter/laravel-keyable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
givebutter / laravel-keyable example snippets
use Illuminate\Database\Eloquent\Model;
use Givebutter\LaravelKeyable\Keyable;
class Account extends Model
{
use Keyable;
// ...
}
$newApiKey = $keyable->createApiKey();
$newApiKey->plainTextApiKey // This is the key you should use to authenticate requests
$newApiKey->apiKey // The instance of ApiKey just created
$myApiKey = ApiKey::create([
'keyable_id' => $account->getKey(),
'keyable_type' => Account::class,
'name' => 'My api key',
]);
$myApiKey->plainTextApikey // Token to be used to authenticate requests
use App\Http\Controllers\Controller;
class FooController extends Controller {
public function index(Request $request)
{
$model = $request->keyable;
// ...
}
}
namespace App\Http\Controllers;
// ...
use Givebutter\LaravelKeyable\Auth\AuthorizesKeyableRequests;
class Controller extends BaseController
{
use AuthorizesKeyableRequests;
}
namespace App\Policies\KeyablePolicies;
use App\Models\Post;
use Illuminate\Database\Eloquent\Model;
use Givebutter\LaravelKeyable\Models\ApiKey;
class PostPolicy {
public function view(ApiKey $apiKey, Model $keyable, Post $post) {
return !is_null($keyable->posts()->find($post->id));
}
}
namespace App\Providers;
// ...
use App\Models\Post;
use App\Policies\KeyablePolicies\PostPolicy;
use Givebutter\LaravelKeyable\Facades\Keyable;
class AuthServiceProvider extends ServiceProvider
{
// ...
protected $keyablePolicies = [
Post::class => PostPolicy::class
];
public function boot(GateContract $gate)
{
// ...
Keyable::registerKeyablePolicies($this->keyablePolicies);
}
}
namespace App\Http\Controllers\PostController;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller {
public function show(Post $post) {
$this->authorizeKeyable('view', $post);
// ...
}
}
use App\Models\Post;
Route::get('/posts/{post}', function (Post $post) {
return $post;
});
use App\Models\Post;
Route::get('/posts/{post}', function (Post $post) {
return $post;
})->keyableScoped();
use App\Models\Post;
use App\Models\User;
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
return $post;
})->scopeBindings()->keyableScoped();