{"id":75843,"date":"2025-11-27T17:22:49","date_gmt":"2025-11-27T09:22:49","guid":{"rendered":"https:\/\/www.yimenapp.com\/kb-yimen\/?p=75843"},"modified":"2025-11-27T17:22:49","modified_gmt":"2025-11-27T09:22:49","slug":"%e7%94%a8php%e5%81%9a%e7%9a%84app%e6%8e%a8%e8%8d%90%e4%b8%80%e9%97%a8%e6%89%93%e5%8c%85","status":"publish","type":"post","link":"https:\/\/www.yimenapp.com\/kb-yimen\/75843\/","title":{"rendered":"\u7528php\u505a\u7684app\u63a8\u8350\u4e00\u95e8\u6253\u5305"},"content":{"rendered":"<p>PHP\uff0c\u5168\u79f0\u4e3a\u201cHypertext Preprocessor\u201d\uff0c\u662f\u4e00\u79cd\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8eWeb\u5f00\u53d1\u7684\u811a\u672c\u8bed\u8a00\u3002\u5b83\u4e0d\u9700\u8981\u7f16\u8bd1\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Web\u670d\u52a1\u5668\u4e0a\u6267\u884c\u3002\u5728\u5f00\u53d1App\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PHP\u6765\u6784\u5efa\u57fa\u4e8eWeb\u7684<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f\/\" title=\"\u5e94\u7528\u7a0b\u5e8f\" target=\"_blank\">\u5e94\u7528\u7a0b\u5e8f<\/a><\/span>\uff0c\u7279\u522b\u662f\u5728\u5f00\u53d1\u4e2d\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u57fa\u4e8ePHP\u7684App\u4e00\u822c\u662f\u4f7f\u7528Web<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e6%a1%86%e6%9e%b6\/\" title=\"\u6846\u67b6\" target=\"_blank\">\u6846\u67b6<\/a><\/span>\u8fdb\u884c\u5f00\u53d1\uff0c\u6bd4\u5982Laravel\uff0cSymfony\u7b49\u7b49\uff0c\u8fd9\u4e9b\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u642d\u5efa\u5e94\u7528\u7a0b\u5e8f\u3001\u5b9a\u4e49<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e8%b7%af%e7%94%b1\/\" title=\"\u8def\u7531\" target=\"_blank\">\u8def\u7531<\/a><\/span>\u3001\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7b49\u7b49\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u4e86\u89e3\u5982\u4f55\u57fa\u4e8ePHP\u6784\u5efaApp\u3002<\/p>\n<p>\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684API\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Lumen\u6846\u67b6\u6784\u5efa\u4e00\u4e2a\u7535\u5546App\u540e\u53f0API\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u642d\u5efa\u597dLumen\u6846\u67b6\u7684\u73af\u5883\uff0c\u5176\u4e2d\u5305\u62ecComposer\u3001Lumen\u6846\u67b6\u672c\u8eab\u3001\u8def\u7531\u914d\u7f6e\u7b49\u3002<\/p>\n<p>\u5728\u5b8c\u6210\u4e86\u73af\u5883\u642d\u5efa\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684API\u63a5\u53e3\uff0c\u4f8b\u5982\u83b7\u53d6\u5546<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.myapp.ltd\/ag\/8\/sucai\/23.jpg\" \/><\/figure><\/p>\n<p>\u54c1\u5217\u8868\u7684API\u3002\u9996\u5148\uff0c\u5728\u8def\u7531\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5bf9\u5e94\u7684\u8def\u7531\u4fe1\u606f\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<\/p>\n<p>$router-&gt;get(&#8216;\/products&#8217;, &#8216;ProductController@index&#8217;);<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u628a\u8bf7\u6c42\u65b9\u5f0f\u4e3aGET\u3001\u8def\u5f84\u4e3a\/products\u7684\u8bf7\u6c42\u6620\u5c04\u5230ProductController@index\u65b9\u6cd5\u4e0a\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u5728ProductController.php\u6587\u4ef6\u4e2d\u5b9e\u73b0index\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<\/p>\n<p>namespace App\\Http\\Controllers;<\/p>\n<p>use App\\Product;<\/p>\n<p>class ProductController extends Controller {<\/p>\n<p>    public function index(){<\/p>\n<p>        \/\/ \u83b7\u53d6\u6240\u6709\u5546\u54c1\u5217\u8868<\/p>\n<p>        $products = Product::all();<\/p>\n<p>        return response()-&gt;json($products);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7Product::all()\u65b9\u6cd5\u83b7\u53d6\u4e86\u6240\u6709\u5546\u54c1\u5217\u8868\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u6700\u540e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u8fd9\u53ea\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u5b9e\u9645\u4e0a\uff0c\u4e00\u4e2a\u5b8c\u6574\u7684App\u9700\u8981\u66f4\u591a\u7684\u529f\u80fd\u6a21\u5757\uff0c\u6bd4\u5982\u7528\u6237\u8ba4\u8bc1\u3001\u5546\u54c1\u7ba1\u7406\u3001\u8ba2\u5355\u7ba1\u7406\u7b49\u7b49\u3002\u5728\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u5b8c\u6210\u4e00\u4e2a\u7528\u6237\u8ba4\u8bc1\u6a21\u5757\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5728Lumen\u6846\u67b6\u4e2d\u96c6\u6210Auth\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5illuminate\/auth\u7ec4\u4ef6\u6765\u5b9e\u73b0\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<p>composer require illuminate\/auth<\/p>\n<p>&#8220;`<\/p>\n<p>\u63a5\u7740\uff0c\u5728config\/<span class=\"wpcom_keyword_link\"><a href=\"https:\/\/www.yimenapp.com\" target=\"_blank\" title=\"\u5f00\u53d1app\u4ece\u4e00\u95e8\u5f00\u59cb\">app<\/a><\/span>.php\u6587\u4ef6\u4e2d\u6dfb\u52a0AuthServiceProvider\u7684\u914d\u7f6e\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<p>$app-&gt;register(Illuminate\\Auth\\AuthServiceProvider::class);<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\uff0c\u5728app\/Http\/Controllers\/Auth\/LoginController.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u767b\u5f55\u903b\u8f91\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<\/p>\n<p>namespace App\\Http\\Controllers\\Auth;<\/p>\n<p>use Illuminate\\Auth\\AuthManager;<\/p>\n<p>use Illuminate\\Http\\Request;<\/p>\n<p>use Illuminate\\Support\\Facades\\Auth;<\/p>\n<p>use Laravel\\Lumen\\Routing\\Controller as BaseController;<\/p>\n<p>class LoginControll<a href=\"https:\/\/www.zhidianwl.com\/\">\u7f51\u7ad9\u505aapp<\/a>er extends BaseController<\/p>\n<p>{<\/p>\n<p>    protected $auth;<\/p>\n<p>    public function __construct(AuthManager $authManager)<\/p>\n<p>    {<\/p>\n<p>        $this-&gt;auth = $authManager-&gt;guard();<\/p>\n<p>    }<\/p>\n<p>    public function login(Request $request)<\/p>\n<p>    {<\/p>\n<p>        $credentials = $request-&gt;only(&#8216;username&#8217;, &#8216;password&#8217;);<\/p>\n<p>        if ($this-&gt;auth-&gt;attempt($credentials)) {<\/p>\n<p>            $user = $this-&gt;auth-&gt;user();<\/p>\n<p>            return response()-&gt;json([&#8216;token&#8217; =&gt; $user-&gt;api_token], 200);<\/p>\n<p>        }<\/p>\n<p>        return response()-&gt;json([&#8216;error&#8217; =&gt; &#8216;Unauthorized&#8217;], 401);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u8bf7\u6c42\u4e2d\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u63d0\u53d6\u51fa\u6765\uff0c\u7136\u540e\u901a\u8fc7$this-&gt;auth-&gt;<a href=\"https:\/\/www.zhidianwl.com\/\">\u7f51\u9875\u505aapp<\/a>attempt\u65b9\u6cd5\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u3002\u5982\u679c\u8ba4\u8bc1\u901a\u8fc7\uff0c\u5219\u8fd4\u56de\u7528\u6237\u7684API Token\uff0c\u5426\u5219\u8fd4\u56de401\u7684HTTP\u72b6\u6001\u7801\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5728app\/User.php\u6587\u4ef6\u4e2d\u6dfb\u52a0api_token\u5b57\u6bb5\uff1a<\/p>\n<p>&#8220;`php<\/p>\n<\/p>\n<p>namespace App;<\/p>\n<p>use Illuminate\\Foundation\\Auth\\User as Authenticatable;<\/p>\n<p>class User extends Authenticatable<\/p>\n<p>{<\/p>\n<p>    protected $fillable = [<\/p>\n<p>        &#8216;name&#8217;, &#8217;email&#8217;, &#8216;password&#8217;, &#8216;api_token&#8217;,<\/p>\n<p>    ];<\/p>\n<p>    protected $hidden = [<\/p>\n<p>        &#8216;password&#8217;, &#8216;api_token&#8217;,<\/p>\n<p>    ];<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7\u6dfb\u52a0api_token\u5b57\u6bb5\u5b9e\u73b0\u8ba4\u8bc1\u4fe1\u606f\u7684\u6301\u4e45\u5316\uff0c\u5e76\u5c06\u5176\u4eceUser\u6a21\u578b\u7684hidden\u5c5e\u6027\u4e2d\u6392\u9664\uff0c\u907f\u514d\u5728API\u54cd\u5e94\u4e2d\u6cc4\u9732\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u8ba4\u8bc1\u6a21\u5757\uff0c\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u5176\u4ed6\u529f\u80fd\u6a21\u5757\uff0c\u6bd4\u5982\u5546\u54c1\u7ba1\u7406\u3001\u8ba2\u5355\u7ba1\u7406\u3001\u652f\u4ed8\u63a5\u53e3\u7b49\u7b49\u3002<\/p>\n<p>\u603b\u7ed3\u6765\u8bf4\uff0c\u57fa\u4e8ePHP\u6784\u5efaApp\u53ef\u80fd\u4f1a\u53d7\u5230\u4e00\u4e9b\u9650\u5236\uff0c\u6bd4\u5982\u9700\u8981\u5728Web\u670d\u52a1\u5668\u4e0a\u6267\u884c\u3001\u9700\u8981\u7ecf\u5e38\u8fdb\u884c\u540c\u6b65\u64cd\u4f5c\u7b49\u7b49\u3002\u4f46\u662f\uff0c\u5728\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u65b9\u9762\uff0cPHP\u975e\u5e38\u9002\u5408\u7528\u6765\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u6216\u8005API\u63a5\u53e3\uff0c\u5728\u901f\u5ea6\u3001\u7a33\u5b9a\u6027\u65b9\u9762\u90fd\u6709\u7740\u826f\u597d\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\uff0c\u5168\u79f0\u4e3a\u201cHypertext Preprocessor\u201d\uff0c\u662f\u4e00\u79cd\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8eWeb\u5f00\u53d1\u7684\u811a\u672c\u8bed\u8a00\u3002\u5b83\u4e0d\u9700\u8981\u7f16\u8bd1\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Web\u670d\u52a1\u5668\u4e0a\u6267\u884c\u3002\u5728\u5f00\u53d1App\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PHP\u6765\u6784\u5efa\u57fa\u4e8eWe<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[34563,2445,3242,50055,11347],"special":[],"attr":[],"class_list":["post-75843","post","type-post","status-publish","format-standard","hentry","category-www","tag-app","tag-2445","tag-3242","tag-angularapp","tag-11347","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/75843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/comments?post=75843"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/75843\/revisions"}],"predecessor-version":[{"id":75867,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/75843\/revisions\/75867"}],"wp:attachment":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/media?parent=75843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/categories?post=75843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/tags?post=75843"},{"taxonomy":"special","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/special?post=75843"},{"taxonomy":"attr","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/attr?post=75843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}