composer create-project laravel/laravel recurly --prefer-dist
"machuga/authority": "dev-develop", "machuga/authority-l4" : "dev-master", "recurly/recurly-client": "2.1.*@dev"
php artisan migrate:make create_users_table
public function up() { Schema::create('users', function($table) { $table->increments('id'); $table->string('email')->unique(); $table->string('name'); $table->string('password'); $table->timestamps(); }); } public function down() { Schema::drop('users'); }
php artisan migrate
'Authority\AuthorityL4\AuthorityL4ServiceProvider',
'Authority' => 'Authority\AuthorityL4\Facades\Authority',
php artisan config:publish machuga/authority-l4
php artisan migrate --package="machuga/authority-l4"
<?php class Role extends Eloquent { }
<?php class Permission extends Eloquent { }
public function roles() { return $this->belongsToMany('Role'); } public function permissions() { return $this->hasMany('Permission'); } public function hasRole($key) { foreach($this->roles as $role){ if ($role->name === $key) { return true; } } return false; }
<?php class DatabaseSeeder extends Seeder { public function run() { Eloquent::unguard(); $this->call('UserTableSeeder'); $this->command->info('User table seeded!'); $this->call('RoleTableSeeder'); $this->command->info('Role table seeded!'); } } class UserTableSeeder extends Seeder { public function run() { DB::table('users')->delete(); User::create(array( 'email' => 'joe.bloggs@example.com', 'name' => 'Joe Bloggs', 'password' => Hash::make('password') )); } } class RoleTableSeeder extends Seeder { public function run() { DB::table('roles')->delete(); Role::create(array('name' => 'admin')); Role::create(array('name' => 'pending')); Role::create(array('name' => 'member')); Role::create(array('name' => 'bronze')); Role::create(array('name' => 'silver')); Role::create(array('name' => 'gold')); } }
php artisan db:seed
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Subscription Site Tutorial</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <meta name="author" content=""> <!-- Le styles --> <link href="/css/bootstrap.css" rel="stylesheet"> <link href="/css/style.css" rel="stylesheet"> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="brand" href="#">Subscription Site Tutorial</a> <div class="nav-collapse collapse"> <ul class="nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#contact">Contact</a></li> </ul> </div><!--/.nav-collapse --> </div> </div> </div> <div class="container"> @if(Session::has('success')) <div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">Ă—</button> {{ Session::get('success') }} </div> @endif @if(Session::has('error')) <div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">Ă—</button> {{ Session::get('error') }} </div> @endif @yield('content') </div> <!-- /container --> <!-- Le javascript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="/js/libs/bootstrap.min.js"></script> </body> </html>
body { padding-top: 50px; } p.logged-in { color: white; margin-top: 0.5em; }
@extends('layouts.default') @section('content') <h1>Subscription Site Tutorial</h1> @stop
Route::get('/', function() { return View::make('home/index'); });
Route::get('/auth/login', function() { return View::make('auth/login'); });
@extends('layouts.default') @section('content') <h1>Please Log in</h1> {{ Form::open(array('url' => 'auth/login')) }} {{ Form::label('email', 'E-Mail Address') }} {{ Form::text('email') }} {{ Form::label('password', 'Password') }} {{ Form::password('password') }} <div class="form-actions"> {{ Form::submit('Login', array('class' => 'btn btn-primary')) }} </div> {{ Form::close() }} <p>Not a member? <a href="/user/register">Register here</a>.</p> @stop
Route::post('/auth/login', function() { $email = Input::get('email'); $password = Input::get('password'); if (Auth::attempt(array('email' => $email, 'password' => $password))) { return Redirect::to('/')->with('success', 'You have been logged in'); } else { return Redirect::to('auth/login')->with('error', 'Login Failed'); } return View::make('auth/login'); });
Route::get('/auth/logout', function() { Auth::logout(); return Redirect::to('/')->with('success', 'You have successfully logged out'); });
Route::get('/user/register', function() { return View::make('user/register/index'); });
@extends('layouts.default') @section('content') {{ Form::open(array('url' => 'user/register')) }} {{ Form::label('email', 'E-Mail Address') }} {{ Form::text('email') }} {{ $errors->first('email') }} {{ Form::label('name', 'Your name') }} {{ Form::text('name') }} {{ $errors->first('name') }} {{ Form::label('password', 'Password') }} {{ Form::password('password') }} {{ $errors->first('password') }} {{ Form::label('password_confirmation', 'Repeat') }} {{ Form::password('password_confirmation') }} <div class="form-actions"> {{ Form::submit('Register', array('class' => 'btn btn-primary')) }} </div> {{ Form::close() }} @stop
Route::post('/user/register', function() { $validator = Validator::make( Input::all(), array( 'name' => array('required', 'min:5'), 'email' => array('required', 'email', 'unique:users'), 'password' => array('required', 'confirmed') ) ); if ($validator->passes()) { $user = new User(); $user->name = Input::get('name'); $user->email = Input::get('email'); $user->password = Hash::make(Input::get('password')); $user->save(); $role_pending = Role::where('name', '=', 'pending')->first(); $user->roles()->attach($role_pending); Auth::login($user); return Redirect::to('/')->with( 'success', 'Welcome to the site, . Auth::user()->name . '!' ); } else { return Redirect::to('user/register')->with( 'error', 'Please correct the following errors:' )->withErrors($validator); } });
Source: https://habr.com/ru/post/195690/
All Articles