{"id":1457,"date":"2015-05-07T17:27:26","date_gmt":"2015-05-07T09:27:26","guid":{"rendered":"http:\/\/www.youthtribe.com\/?p=1457"},"modified":"2016-12-21T20:15:21","modified_gmt":"2016-12-21T12:15:21","slug":"laravel-5-0-%e5%ae%9e%e7%8e%b0%e9%bb%98%e8%ae%a4%e7%9a%84%e5%a4%9a%e8%a1%a8auth%e8%ae%a4%e8%af%81%ef%bc%8c%e5%a6%82%e5%89%8d%e5%90%8e%e5%8f%b0%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.youthtribe.com\/archives\/1457","title":{"rendered":"laravel 5.0 \u5b9e\u73b0\u9ed8\u8ba4\u7684\u591a\u8868auth\u8ba4\u8bc1\uff0c\u5982\u524d\u540e\u53f0\u8868"},"content":{"rendered":"
\n

\u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01<\/strong><\/p>\n

\u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01<\/strong><\/p>\n

\u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01<\/strong><\/p>\n

\u7814\u7a76\u4e00\u7ffb\uff0c\u7adf\u7136\u6210\u4e86\uff0c\u4e0d\u8fc7\u662f\u61f5\u61f5\u61c2\u61c2\u554a\u3002\u3002<\/p>\n

\u76ee\u7684\u662f\u5b9e\u73b0\u524d\u540e\u53f0\u7528\u6237\u7684\u5206\u522b\u767b\u5f55\uff0c\u8fd9\u6837\u5c31\u5bf9\u5e94\u4e24\u4e2a\u8868\u4e86\u3002\u3002laravel5\u9ed8\u8ba4\u662f\u5bf9\u5e94users\u8868\uff0c\u6211\u518d\u5efa\u7acb\u4e00\u4e2aadmins\u8868\uff0c\u7528\u6765\u540e\u53f0\u767b\u5f55\u3002
\n\u76f4\u63a5\u4e0a\u5427\uff0c\u6700\u4e3b\u8981\u5c31\u662f\u6709\u4e24\u4e2aconfig\u8bbe\u7f6e\uff1a<\/p>\n

\t\r\n\tConfig::set('auth.model', 'App\\Model\\admin\\admin');\r\n\tConfig::set('auth.table', 'admins');\r\n<\/pre>\n

\u4e00\u4e2a\u662f\u4fee\u6539\u767b\u5f55\u6a21\u578b\uff0c\u4e00\u4e2a\u662f\u5bf9\u5e94\u7684\u8ba4\u8bc1\u8868\u3002<\/p>\n


\n

1.\u5efa\u7acb\u4e2d\u95f4\u4ef6 make:middleware AdminAuth<\/p>\n

\u7136\u540e\u8bb0\u5f97\u5728\u4f60\u7684\u8def\u7531\u7ec4\u4e2d\u6dfb\u52a0\u8fd9\u4eba\u8fc7\u6ee4\u4e2d\u95f4\u4ef6<\/strong><\/p>\n

\u8fd9\u662f\u7528\u6765\u8fc7\u6ee4\u7684\uff0c\u6ca1\u6709\u767b\u5f55\u5c31\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\u3002<\/p>\n

\tpublic function handle($request, Closure $next)\r\n\t{\r\n\t\t\/\/\u4fee\u6539\u4e00\u4e0bauth\u7684\u9ed8\u8ba4\u767b\u5f55\u8868\r\n\t\t\/\/echo Auth::getFacadeApplication()->config['auth']['table'];\r\n\t\tConfig::set('auth.model', 'App\\Model\\admin\\admin');\r\n\t\tConfig::set('auth.table', 'admins');\r\n\t\t\/\/dd(Auth::getFacadeApplication()->config['auth']['table']);\r\n \t\tif(Auth::check()){\r\n\t\t\t\/\/\u5df2\u7ecf\u767b\u5f55\t\r\n\t\t\t\/\/dd('s');\t\r\n\t\t}\r\n\t\telse{\r\n\t\t\t\/\/\u8fd8\u6ca1\u6709\u767b\u5f55\r\n\t\t\t\/\/dd('not login');\r\n\t\t\treturn redirect()->guest('admin\/login');\r\n\t\t}\r\n\t\t\r\n\t\treturn $next($request);\r\n\t}\r\n<\/pre>\n

\u4fee\u6539Kernel\u6587\u4ef6\u4e2d $routeMiddleware\u7684\u6570\u7ec4\uff0c\u6dfb\u52a0\u547d\u540d\u6211\u4eec\u7684\u4e2d\u95f4\u4ef6<\/p>\n

\r\n\t\t\/\/\u540e\u53f0\u7ba1\u7406\u7684\u4e2d\u95f4\u4ef6\r\n\t\t'adminauth' => 'App\\Http\\Middleware\\AdminAuth'\r\n\r\n<\/pre>\n

2.\u5b9e\u73b0\u767b\u5f55\u9a8c\u8bc1\u64cd\u4f5c<\/p>\n

public function loginHandle()\r\n\t{\r\n\t\t\/\/\u4e0b\u8fb9\u4e24\u4e2a\u8bbe\u7f6e\u5f88\u91cd\u8981\r\n\t\tConfig::set('auth.model', 'App\\Model\\admin\\admin');\r\n\t\tConfig::set('auth.table', 'admins');\r\n\t\t\/\/\r\n\t\t$username = Input::get('username');\r\n\t\t$password = Input::get('password');\r\n\t\t$rememberme = true;\r\n\t\tif (Input::get('rememberme')){\r\n\t\t\t$rememberme = true;\r\n\t\t}\r\n\t\telse{\r\n\t\t\t$rememberme = false;\r\n\t\t}\r\n\t\tif(Auth::attempt([\r\n\t\t\t'username' => $username,'password' => $password\r\n\t\t],$rememberme))\t\t\t\t\r\n\t\t{\r\n\t\t\t\/\/\u767b\u5f55\u6210\u529f\r\n\t\t\treturn redirect(action('admin\\AdminController@index'));\r\n\t\t}\r\n\t\telse {\r\n\t\t\t\/\/\u767b\u5f55\u5931\u8d25\r\n\t\t\treturn redirect(action('admin\\AdminController@login'))\r\n\t\t\t->withErrors(\"\u7528\u6237\u540d\u6216\u8005\u5bc6\u7801\u4e0d\u6b63\u786e\");\r\n\t\t}\r\n\t\t\/\/\r\n\t}\r\n<\/pre>\n

3.\u5efa\u7acb\u5e76\u4fee\u6539 admin\u6a21\u578b\u3002\u56e0\u4e3a\u7528artisan\u751f\u6210\u7684\u9ed8\u8ba4\u6a21\u578b\u5176\u5b9e\u662f\u6ca1\u6709\u9a8c\u8bc1\u529f\u80fd\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u751f\u6210\u540e\uff0c\u76f4\u63a5\u7167\u642c \u6846\u67b6\u81ea\u5e26\u7684 users \u6a21\u578b\uff0c\u7136\u540e\u4fee\u6539\u4e00\u4e0b<\/p>\n

protected $table = 'admins';\r\n\r\n<\/pre>\n

\u9644\u6211\u7684admin \u6a21\u578b<\/p>\n

<?php namespace App\\Model\\admin;\r\n\r\nuse Illuminate\\Auth\\Authenticatable;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\nuse Illuminate\\Auth\\Passwords\\CanResetPassword;\r\nuse Illuminate\\Contracts\\Auth\\Authenticatable as AuthenticatableContract;\r\nuse Illuminate\\Contracts\\Auth\\CanResetPassword as CanResetPasswordContract;\r\n\r\nclass Admin extends Model implements AuthenticatableContract, CanResetPasswordContract {\r\n\r\n\t\/\/\r\n\tuse Authenticatable, CanResetPassword;\r\n\t\r\n\t\/**\r\n\t * The database table used by the model.\r\n\t *\r\n\t * @var string\r\n\t *\/\r\n\tprotected $table = 'admins';\r\n\t\r\n\t\/**\r\n\t * The attributes that are mass assignable.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $fillable = ['name', 'email', 'password'];\r\n\t\r\n\t\/**\r\n\t * The attributes excluded from the model's JSON form.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $hidden = ['password', 'remember_token'];\r\n\t\r\n}\r\n<\/pre>\n

\u5c31\u597d\u4e86\u3002\u3002<\/p>\n

\u597d\u50cf\u5c31\u662f\u8fd9\u6837\u5427\uff0c\u4f60\u4eec\u8bd5\u8bd5\uff0c\u6709\u95ee\u9898\u7559\u8a00\u3002\u6211\u8fd9\u662fok\u4e86\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01 \u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01 \u4e0d\u8981\u770b\u8fd9\u4e2a\u6587\u7ae0\u4e86\u3002\u5199\u7684\u6839\u672c\u5c31\u4e0d\u5bf9\uff01 \u7814\u7a76\u4e00\u7ffb\uff0c\u7adf\u7136\u6210\u4e86\uff0c\u4e0d\u8fc7\u662f\u61f5\u61f5\u61c2\u61c2\u554a\u3002\u3002 \u76ee\u7684\u662f\u5b9e\u73b0\u524d\u540e\u53f0\u7528\u6237\u7684\u5206\u522b\u767b\u5f55\uff0c\u8fd9\u6837\u5c31\u5bf9\u5e94\u4e24\u4e2a\u8868\u4e86\u3002\u3002laravel5\u9ed8\u8ba4\u662f\u5bf9\u5e94users\u8868\uff0c\u6211\u518d\u5efa\u7acb\u4e00\u4e2aadmins\u8868\uff0c\u7528\u6765\u540e\u53f0\u767b\u5f55\u3002 \u76f4\u63a5\u4e0a\u5427\uff0c\u6700\u4e3b\u8981\u5c31\u662f\u6709\u4e24\u4e2aconfig\u8bbe\u7f6e\uff1a Config::set(‘auth.model’, ‘App\\Model\\admin\\admin’); Config::set(‘auth.table’, ‘admins’); \u4e00\u4e2a\u662f\u4fee\u6539\u767b\u5f55\u6a21\u578b\uff0c\u4e00\u4e2a\u662f\u5bf9\u5e94\u7684\u8ba4\u8bc1\u8868\u3002 1.\u5efa\u7acb\u4e2d\u95f4\u4ef6 make:middleware<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,261,9],"tags":[260,262],"_links":{"self":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1457"}],"collection":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/comments?post=1457"}],"version-history":[{"count":7,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1457\/revisions"}],"predecessor-version":[{"id":1615,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/1457\/revisions\/1615"}],"wp:attachment":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/media?parent=1457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/categories?post=1457"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/tags?post=1457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}