Laravel实战

Project

composer create-project laravel/laravel laravel-system-admin && cd laravel-system-admin

vim .env
APP_SALT="salt"

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD="password"

IDE

composer require --dev barryvdh/laravel-ide-helper

vim app/Providers/AppServiceProvider.php
app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}
php artisan ide-helper:generate

Hasher

mkdir -p app/Services/Auth

vim app/Services/Auth/PasswordHasher.php
salt = env('APP_SALT');
    }

    public function make($value, array $options = [])
    {
        return parent::make(hash('sha256', $value . $this->salt));
    }

    public function check($value, $hashedValue, array $options = [])
    {
        return parent::check(hash('sha256', $value . $this->salt), $hashedValue);
    }
}

Provider

vim app/Providers/AuthServiceProvider.php
 'App\Policies\ModelPolicy',
    ];

    public function boot()
    {
        $this->registerPolicies();

        Auth::guard('admin')->getProvider()->setHasher($this->app['passwordHasher']);
    }

    public function register()
    {
        $this->app->singleton('passwordHasher', function () {
            return new PasswordHasher();
        });
    }
}

Config

vim config/auth.php
 [
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],

    'providers' => [
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],
    ],
];

Model

php artisan make:model Models/Admin

vim app/Models/Admin.php

Route

vim routes/web.php
 '18020125636',
        'password' => '18020125636'
    ];

    if(! Auth::guard('admin')->attempt($credential)) {
        return [ 'code' => -1 ];
    }

    return [ 'code' => 0 ];
});

CSRF

vim app/Http/Middleware/VerifyCsrfToken.php
  • 测试
php artisan serve

curl -X POST http://127.0.0.1:8000/login | json
{
  "code": 0
}

Redis

docker run --name laravel-system-admin -p 6379:6379 -d redis
composer require predis/predis

vim .env
SESSION_DRIVER=redis

Route

vim routes/web.php
 '18020125636',
        'password' => '18020125636'
    ];

    if(! Auth::guard('admin')->attempt($credential)) {
        return [ 'code' => -1 ];
    }

    $user = Auth::guard('admin')->user();

    return [
        'code' => 0,
        'data' => $user,
    ];
});

Route::group(['middleware' => 'auth:admin'], function () {
    Route::get('/user', function () {
        return 'user';
    });
});
  • 测试
php artisan serve
curl localhost:8000 # Page not found

curl localhost:8000/user # AuthenticationException 

Exception

vim app/Exceptions/Handler.php
json(['message' => $exception->getMessage()], 404);
        } else if ($exception instanceof AuthenticationException) {
            return response()->json(['message' => $exception->getMessage()], 401);
        }

        return parent::render($request, $exception);
    }
}
  • 测试
php artisan serve
curl localhost:8000 | json
{
  "message": ""
}
curl -c cookies -X POST localhost:8000/login | json 
{
  "code": 0,
  "data": {
    "id": 6,
    "created_at": "2018-01-15 09:34:16",
    "updated_at": "2018-07-23 17:55:27",
    "nick_name": "王世新",
    "unique_name": "18020125636",
    "is_active": 1
  }
}
curl -b cookies localhost:8000/user # user

Controller

php artisan make:controller AuthorizationsController

vim app/Http/Controllers/AuthorizationsController.php
 '18020125636',
            'password' => '18020125636'
        ];

        if(! Auth::guard('admin')->attempt($credential)) {
            return [ 'code' => -1 ];
        }

        $user = Auth::guard('admin')->user();

        return [
            'code' => 0,
            'data' => $user,
        ];
    }
}
vim routes/web.php
name('auth.login');;

Route::group(['middleware' => 'auth:admin'], function () {
    Route::get('/user', function () {
        return 'user';
    });
});

Request

php artisan make:request AuthorizationRequest

vim app/Http/Requests/AuthorizationRequest.php
 'required|string',
            'password' => 'required|string|min:6',
        ];
    }
}
vim app/Http/Controllers/AuthorizationsController.php
unique_name;
        $credentials['password'] = $request->password;

        if(! Auth::guard('admin')->attempt($credentials)) {
            return [ 'code' => -1 ];
        }

        $user = Auth::guard('admin')->user();

        return [
            'code' => 0,
            'data' => $user,
        ];
    }
}
vim app/Exceptions/Handler.php
json(['message' => $exception->getMessage()], 404);
        } else if ($exception instanceof AuthenticationException) {
            return response()->json(['message' => $exception->getMessage()], 401);
        } else if ($exception instanceof ValidationException) {
            return response()->json([
                'status_code' => 400,
                'message' => $exception->getMessage(),
            ], 200);
        }

        return parent::render($request, $exception);
    }
}

Resource

php artisan make:resource JsonModel

vim app/Http/Resources/JsonModel.php
 ''];
        $data [self::KEY_CODE] = $code;
        $data [self::KEY_MESSAGE] = $message ?? JsonCode::getCodeMessage($code);
        $data [self::KEY_RESULT] = $variables;

        parent::__construct($data);

        $this->result = $data;
    }

    public function toArray($request)
    {
        return $this->result;
    }
}
vim app/Http/Resources/JsonCode.php
 '成功',
        self::HTTP_METHOD_INVALID => '错误的请求',
        self::HTTP_METHOD_MUST_BE_GET => '必须为GET请求',
        self::HTTP_METHOD_MUST_BE_POST => '必须为POST请求',
        self::HTTP_METHOD_MUST_BE_DELETE => '必须为DELETE请求',
        self::PERMISSION_INVALID=> '没有权限',
        self::TENANT_PERMISSION_INVALID => '没有指挥官权限',
        self::LOGIN_REQUIRED => '未登录',
        self::DATA_INVALID => '数据不正确',
        self::DATA_UNRECOGNIZED => '不被识别的数据类型',
        self::DATA_FORMAT_INVALID => '数据格式不正确',
        self::DATA_FORM_INVALID => '表单数据不正确',
        self::DATA_EXIST => '数据已存在',
        self::DATA_PHONE_EXIST => '电话号码已存在',
        self::DATA_NOT_EXIST => '数据不存在',
        self::DATA_PHONE_NOT_EXIST => '电话号码不存在',
        self::DATA_FIELD_INVALID => '参数错误',
        self::DATA_VERIFY_CODE_INVALID => '验证码不正确',
        self::DATA_VERIFY_CODE_FAILED => '验证码失效',
        self::AUTH_FAILED => '获取用户信息失败',
        self::AUTH_IDENTITY_ABNORMAL => '用户身份异常',
        self::AUTH_IDENTITY_NOT_ACTIVE => '用户未激活',
        self::LOGIN_FAILED => '登录失败',
        self::WEUSER_NOT_EXIST => '微信用户不存在',
        self::PASSPORT_NOT_EXIST => '平台账号不存在',
        self::USERS_NOT_EXIST => '没有可用的帐户',
        self::TENANT_CHECKING => '租户正在审核中',
        self::CLIENT_INVALID => '客户端类型错误',
        self::OPERATE_FAILED => '操作失败',
        self::REACH_THE_MAX => '操作次数已达上限',
        self::RESOURCE_INVALID => '资源无效',
        self::RESOURCE_NOT_EXIST => '资源不存在',
        self::TENANT_NOT_EXIST => '租户不存在',
        self::RESOURCE_EXIST => '资源已存在',
        self::RESOURCE_NOT_MODIFIED => '资源未改变',
        self::RESOURCE_MODIFIED => '资源已改变',
        self::API_INVALID => 'API无效',
        self::DATA_VERIFY_CODE_SEND_FAILED => '验证码发送失败'
    ];

    public static function getCodeMessage($code)
    {
        return isset($code, self::$json_code_messages) ? self::$json_code_messages [$code] : '';
    }
}
vim app/Providers/AppServiceProvider.php
app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}
vim app/Http/Controllers/AuthorizationsController.php
unique_name;
        $credentials['password'] = $request->password;

        if(! Auth::guard('admin')->attempt($credentials)) {
            return [ 'code' => -1 ];
        }

        $user = Auth::guard('admin')->user();

        return new JsonModel($user);
    }
}
  • 测试
php artisan serve
curl -X POST \
  http://127.0.0.1:8000/login \
  -H 'content-type: application/json' \
  -d '{
    "unique_name": "18020125636",
    "password": "18020125636"
}' | json
{
  "XETag": "",
  "XCmdrCode": 0,
  "XCmdrMessage": "成功",
  "XCmdrResult": {
    "id": 6,
    "created_at": "2018-01-15 09:34:16",
    "updated_at": "2018-07-23 17:55:27",
    "nick_name": "王世新",
    "unique_name": "18020125636",
    "is_active": 1
  }
}

Controller

php artisan make:controller MachinesController

vim app/Http/Controllers/MachinesController.php

Route

vim routes/web.php
name('auth.login');

Route::group(['middleware' => 'auth:admin'], function () {
    Route::get('/user', function () {
        return 'user';
    });
});

Route::get('/machines', 'MachinesController@index')->name('machines.index');

Query

vim app/Http/Controllers/MachinesController.php
query('online');
        $count_per_page = $request->query('count_per_page', 10);

        $qb = DB::table('machine AS m')
                ->leftJoin('machine_online_state AS s', 's.machine_id', '=', 'm.id');
        if ($online) {
            $qb->where('s.state', 'online');
        }
        $machines = $qb->where('m.deleted', 0)->paginate($count_per_page);
        return new JsonModel([ 'data_list' => $machines ]);
    }
}

Model

php artisan make:model Models/Category

vim app/Models/Category.php
php artisan make:model Models/Machine

vim app/Models/Machine.php
hasOne('App\Models\Category', 'id', 'category_id');
    }
}
vim app/Http/Controllers/MachinesController.php
query('online');
        $count_per_page = $request->query('count_per_page', 10);

        $qb = Machine::leftJoin('machine_online_state AS s', 's.machine_id', '=', 'machine.id');
        if ($online) {
            $qb->where('s.state', 'online');
        }
        $qb = $qb->where('machine.deleted', 0);
        $paginator = $qb->simplePaginate($count_per_page);

        return new JsonModel([ 'data_list' => $paginator ]);
    }
}

Paginator

vim app/Http/Resources/PaginatorJsonModel.php
resource;
        if (! $resource instanceof LengthAwarePaginator) {
            throw new \InvalidArgumentException("class_name must be instance of ".LengthAwarePaginator::class."!");
        }

        $data = [
            self::KEY_DATA_LIST => $variables,
            self::KEY_TOTAL_ITEM_COUNT => $resource->count(),
            self::KEY_TOTAL_PAGE_COUNT => $resource->lastPage(),
        ];

        parent::__construct($data);
    }
}
php artisan make:resource MachinesResource

vim app/Http/Resources/MachinesResource.php
category;

        return [
            'id' => $machine->id,
            'machine_name' => $machine->machine_name,
            'category' => [
                'id' => $category->id,
                'category_name' => $category->category_name,
            ],
        ];
    }
}
vim app/Http/Controllers/MachinesController.php
query('online');
        $count_per_page = $request->query('count_per_page', 10);

        $qb = Machine::leftJoin('machine_online_state AS s', 's.machine_id', '=', 'machine.id');
        if ($online) {
            $qb->where('s.state', 'online');
        }
        $qb = $qb->where('machine.deleted', 0);
        $paginator = $qb->paginate($count_per_page);

        return new PaginatorJsonModel(MachinesResource::collection($paginator));
    }
}

你可能感兴趣的:(Laravel实战)