PHP code example of givebutter / laravel-keyable

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;

    // ...
}

// ...

protected function mapApiRoutes()
{
    Route::prefix('api')
        ->middleware(['api', 'auth.apikey'])
	->namespace($this->namespace . '\API')
	->group(base_path('routes/api.php'));
}

// ...

$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;

        // ...
    }

}

return $model->foo()->get();



return [

    'allow_empty_models' => true

];



return [

    'mode' => 'header',

    'key' => 'X-Authorization',

];



return [

    'mode' => 'parameter',

    'key' => 'api_key'

];

https://example.com/api/posts?api_key=<key>



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();
bash
php artisan vendor:publish --provider="Givebutter\LaravelKeyable\KeyableServiceProvider"
bash
php artisan migrate
bash
php artisan api-key:generate --id=1 --type="App\Models\Account" --name="My api key"
bash
php artisan api-key:delete --id=12345