Home >Backend Development >PHP Tutorial >Removing the Pain of User Authorization with Sentinel
This article demonstrates building a simple demo application using Slim micro-framework and Cartalyst's Sentinel package for user authorization. Sentinel streamlines user management, including roles, permissions, and authentication, in multi-user applications.
Key Features & Benefits:
Setting up the Environment:
This tutorial utilizes Slim, Vagrant, and Composer. The complete demo code is available on Github. Begin by installing required packages:
<code class="language-bash">composer require slim/slim:~2.0 twig/twig:~1.* cartalyst/sentinel:2.0.* illuminate/database illuminate/events symfony/http-foundation ircmaxell/password-compat</code>
Database Setup:
For database interaction, create the necessary tables. Laravel users can use migrations:
<code class="language-bash">php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider" php artisan migrate</code>
Otherwise, manually execute vendor/cartalyst/sentinel/schema/mysql.sql
after adding your database connection details at the top of the file.
Application Bootstrap (public/index.php
):
<code class="language-php"><?php require_once __DIR__.'/../vendor/autoload.php'; $app = new \Slim\Slim(); //register bindings include_once __DIR__.'/../app/bootstrap/container.php'; include_once __DIR__.'/../app/routes.php'; $app->run();</code>
Container Bindings (app/bootstrap/container.php
):
Configure container bindings for Twig and Eloquent:
<code class="language-php">$app->container->twigLoader = new Twig_Loader_Filesystem(__DIR__.'/../views'); $app->container->twig = new Twig_Environment($app->container->twigLoader, array( 'cache' => false, )); $capsule = new \Illuminate\Database\Capsule\Manager(); $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'capsule', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', ]); $capsule->bootEloquent(); $app->container->sentinel = (new \Cartalyst\Sentinel\Native\Facades\Sentinel())->getSentinel();</code>
Creating Roles:
Define roles and permissions (this code is temporary, run once to populate the database):
<code class="language-php">$app->container->sentinel->getRoleRepository()->createModel()->create(array( 'name' => 'Admin', 'slug' => 'admin', 'permissions' => array( 'user.create' => true, 'user.update' => true, 'user.delete' => true ), )); $app->container->sentinel->getRoleRepository()->createModel()->create(array( 'name' => 'User', 'slug' => 'user', 'permissions' => array( 'user.update' => true ), ));</code>
The remainder of the article details creating signup and login pages, handling user activation, implementing permission checks (using hasAccess()
), and logging out users. The code examples cover routing, user creation, role assignment, activation email sending, and permission-based access control. The article concludes with a FAQ section addressing common Sentinel usage questions.
The above is the detailed content of Removing the Pain of User Authorization with Sentinel. For more information, please follow other related articles on the PHP Chinese website!