{"id":1084,"date":"2016-08-18T11:20:51","date_gmt":"2016-08-18T03:20:51","guid":{"rendered":"http:\/\/www.poloo.org\/?p=1084"},"modified":"2016-08-18T11:28:56","modified_gmt":"2016-08-18T03:28:56","slug":"laravelcollectivehtml-laravel%e7%9a%84html%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=1084","title":{"rendered":"laravelcollective\/html-Laravel\u7684Html\u7ec4\u4ef6"},"content":{"rendered":"<p>illuminate\/html \u4f5c\u4e3a\u4ece 4.* \u7248\u672c\u4e2d\u5206\u652f\u51fa\u6765\u7684\u5bf9\u4e8e html \u548c form \u4fbf\u6377\u5199\u6cd5\u7684\u7c7b\u5e93\u5df2\u7ecf\u5728 5.0.* \u4e2d\u6536\u5230\u4e86\u663e\u8457\u7684\u6b22\u8fce. \u4f46\u662f illuminate\/html \u53ea\u652f\u6301 laravel 5.0.* \u7684\u7248\u672c. \u5347\u7ea7\u5230 5.1 \u4e5f\u53ef\u4ee5\u52c9\u5f3a\u4f7f\u7528. \u65b0\u7684html \u66ff\u4ee3\u65b9\u6848\u4e3a\uff1a laravelcollective\/html \u53c2\u770b\uff1ahttps:\/\/laravelcollective.com\/<\/p>\n<p><!--more--><br \/>\n<strong>\u5b89\u88c5<\/strong><br \/>\n\u9996\u5148\u901a\u8fc7 composer \u6765\u5b89\u88c5\u8fd9\u4e2a \u5305, \u7f16\u8f91\u9879\u76ee\u7684 composer.json \u6587\u4ef6. \u5728 require \u90e8\u5206 \u52a0\u5165 laravelcollective\/html :<\/p>\n<pre class=\"brush:php\">\"require\": {\r\n\"laravelcollective\/html\": \"5.1.*\"\r\n}\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u4ece\u547d\u4ee4\u884c\u66f4\u65b0 composer :<\/p>\n<pre class=\"brush:php\">composer update -vvv\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u6dfb\u52a0 provider \u5230 config\/app.php \u7684 providers \u6570\u7ec4:<\/p>\n<pre class=\"brush:php\">'providers' =&gt; [\r\n\/\/ ...\r\nCollective\\Html\\HtmlServiceProvider::class,\r\n\/\/ ...\r\n],\r\n<\/pre>\n<p>\u6700\u540e \u6dfb\u52a0\u4e24\u4e2a\u7c7b\u94fe\u63a5\u5230 config\/app.php \u7684 aliases \u6570\u7ec4:<\/p>\n<pre class=\"brush:php\">'aliases' =&gt; [\r\n\/\/ ...\r\n'Form' =&gt; Collective\\Html\\FormFacade::class,\r\n'Html' =&gt; Collective\\Html\\HtmlFacade::class,\r\n\/\/ ...\r\n],\r\n<\/pre>\n<p><strong>\u521b\u5efa\u8868\u5355<\/strong><br \/>\n\u6253\u5f00\u8868\u5355<\/p>\n<pre class=\"brush:php\">{!! Form::open(array('url' =&gt; 'foo\/bar')) !!}\r\n\/\/\r\n{!! Form::close() !!}\r\n<\/pre>\n<p>\u9ed8\u8ba4\u662f POST \u65b9\u6cd5, \u4f60\u53ef\u4ee5\u968f\u610f\u6307\u5b9a\u5176\u4ed6\u63a5\u6536\u65b9\u6cd5<\/p>\n<pre class=\"brush:php\">echo Form::open(array('url' =&gt; 'foo\/bar', 'method' =&gt; 'put'))\r\n<\/pre>\n<blockquote><p>Note: HTML \u8868\u5355\u4ec5\u4ec5\u652f\u6301 POST \u548c GET\u65b9\u6cd5, PUT \u548cDELETE \u65b9\u6cd5\u5c06\u4f1a\u4f7f\u7528\u4e00\u4e2a\u9690\u85cf\u57df_method \u6dfb\u52a0\u5230form \u8868\u5355\u4e2d\u6765\u6b3a\u9a97\u5b9e\u73b0<\/p><\/blockquote>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u6307\u5b9a\u7684\u63a7\u5236\u5668@\u52a8\u4f5c \u6216\u8005\u547d\u540d\u7684\u8def\u7531\u6765\u521b\u5efa\u8868\u5355<\/p>\n<pre class=\"brush:php\">echo Form::open(array('route' =&gt; 'route.name'))\r\n\r\necho Form::open(array('action' =&gt; 'Controller@method'))\r\n<\/pre>\n<p>\u540c\u6837\u4e5f\u53ef\u4ee5\u5411\u8def\u7531\u4e2d\u4f20\u5165\u53c2\u6570.<\/p>\n<pre class=\"brush:php\">echo Form::open(array('route' =&gt; array('route.name', $user-&gt;id)))\r\n\r\necho Form::open(array('action' =&gt; array('Controller@method', $user-&gt;id)))\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u7684\u8868\u5355\u9700\u8981\u652f\u6301\u6587\u4ef6\u4e0a\u4f20, \u5728\u6570\u7ec4\u4e2d\u6dfb\u52a0 \u4e00\u4e2a files \u914d\u7f6e\u9879.<\/p>\n<pre class=\"brush:php\">echo Form::open(array('url' =&gt; 'foo\/bar', 'files' =&gt; true))\r\n<\/pre>\n<p><strong>CSRF \u4fdd\u62a4<\/strong><br \/>\n\u5411\u8868\u5355\u4e2d\u6dfb\u52a0 CSRF Token<br \/>\nLaravel \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u9632\u6b62\u4f60\u7684\u5e94\u7528\u906d\u53d7\u8de8\u7ad9\u653b\u51fb. \u9996\u5148\u4f1a\u5728\u4f60\u7684session \u4e2d\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 token, \u5982\u679c\u4f60\u4f7f\u7528 Form::open \u65b9\u6cd5\u5e76\u4e14\u63d0\u4ea4\u65b9\u6cd5\u662f POST, PUT\u6216\u8005\u662f DELETE, CSRF token \u5c06\u4f1a\u81ea\u52a8\u7684\u6dfb\u52a0\u5230\u4f60\u7684form \u8868\u5355\u7684\u9690\u85cf\u57df\u4e2d. \u6362\u79cd\u65b9\u6cd5 \u5982\u679c\u4f60\u50cf\u81ea\u5df1\u751f\u6210 CSRF token \u5b57\u6bb5, \u4f60\u53ef\u4ee5\u4f7f\u7528 token \u65b9\u6cd5.<\/p>\n<pre class=\"brush:php\">echo Form::token();\r\n<\/pre>\n<p>\u7ed9\u8def\u7531\u6dfb\u52a0 CSRF \u8fc7\u6ee4\u5668<\/p>\n<pre class=\"brush:php\">Route::post('profile', array('before' =&gt; 'csrf', function()\r\n{\r\n\/\/\r\n}));\r\n<\/pre>\n<p><strong>\u8868\u5355\u6a21\u578b\u7ed1\u5b9a<\/strong><br \/>\n\u7ed9\u8868\u5355\u7ed1\u5b9a\u6a21\u578b<br \/>\n\u901a\u5e38, \u4f60\u9700\u8981\u60f3\u8868\u5355\u4e2d\u586b\u5165\u6765\u81ea\u6570\u636e\u5e93\u6a21\u578b\u7684\u6570\u636e. \u60f3\u8fd9\u6837\u505a\u4f60\u53ef\u4ee5\u4f7f\u7528 Form::model \u65b9\u6cd5.<\/p>\n<pre class=\"brush:php\">echo Form::model($user, array('route' =&gt; array('user.update', $user-&gt;id)))\r\n<\/pre>\n<p>\u73b0\u5728\u5f53\u4f60\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u8868\u5355\u5143\u7d20, \u4f8b\u5982\u6587\u672c\u8f93\u5165\u6846. Model \u7684\u503c\u5c06\u4f1a\u81ea\u52a8\u5339\u914d\u5e76\u4e14\u586b\u5199\u5230\u76f8\u5173\u7684\u8868\u5355\u5b57\u6bb5\u4e2d.\u4f8b\u5982. \u4e00\u4e2a\u6587\u672c\u8f93\u5165\u6846\u7684 name \u662f email\u8fd9\u4e2a\u5b57\u6bb5\u5c06\u4f1a\u7528 \u7528\u6237 Model \u7684 email \u5c5e\u6027\u6765\u586b\u5145\u5e76\u4e14\u8bbe\u7f6e. \u5f53\u7136, \u8fd8\u6709\u5176\u4ed6\u7528\u6cd5. \u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u5728session \u95ea\u5b58\u6570\u636e\u4e2d \u4e5f\u5b58\u5728\u8fd9\u4e2a\u540d\u5b57, \u8fd9\u4e2a\u5c06\u4f1a\u8986\u76d6\u6a21\u578b\u4e2d\u7684\u8fd9\u4e2a\u5b57\u6bb5\u503c. \u4f18\u5148\u7ea7\u662f\u8fd9\u4e2a\u6837\u5b50\u7684:<\/p>\n<ul>\n<li>Session Flash Data (Old Input) [session \u95ea\u5b58 \/ \u8001\u7684\u8f93\u5165\u6570\u636e ]<\/li>\n<li>Explicitly Passed Value [\u8f93\u5165\u503c]<\/li>\n<li>Model Attribute Data [\u6a21\u578b\u5c5e\u6027\u503c]<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u53ef\u4ee5\u8ba9\u4f60\u5feb\u901f\u7684\u4f7f\u7528\u6a21\u578b\u6570\u636e\u6765\u521b\u5efa\u8868\u5355, \u4e5f\u80fd\u8f7b\u677e\u7684\u5728\u670d\u52a1\u5668\u6821\u9a8c\u9519\u8bef\u4e4b\u540e\u91cd\u65b0\u53d1\u5e03\u8868\u5355.<\/p>\n<blockquote><p>Note: \u4f7f\u7528 Form::model\u65b9\u6cd5\u7684\u65f6\u5019\u4e00\u5b9a\u8981\u4f7f\u7528 Form::close\u6765\u5173\u95ed\u8868\u5355!<\/p><\/blockquote>\n<p><strong>\u6807\u7b7e<\/strong><br \/>\n\u751f\u6210\u6807\u7b7e\u5143\u7d20<\/p>\n<pre class=\"brush:php\">echo Form::label('email', 'E-Mail Address');\r\n<\/pre>\n<p>\u6307\u5b9a\u989d\u5916\u7684 html \u5c5e\u6027<\/p>\n<pre class=\"brush:php\">echo Form::label('email', 'E-Mail Address', array('class' =&gt; 'awesome'));\r\n<\/pre>\n<blockquote><p>Note: \u5728\u521b\u5efa\u4e86\u4e00\u4e2a\u6807\u7b7e\u4e4b\u540e, \u5982\u679c\u6709\u521b\u5efa\u7684\u8868\u5355\u5143\u7d20\u7684 name \u503c\u548c label \u7684 name \u503c\u76f8\u7b26\u7684\u8bdd, \u5c06\u4f1a\u81ea\u52a8\u5728 \u8868\u5355\u5143\u7d20 \u4e2d\u81ea\u52a8\u5339\u914d\u589e\u52a0 id \u5c5e\u6027. id \u7684\u503c\u5c31\u662f label \u7684name \u503c.<\/p><\/blockquote>\n<p><strong>\u6587\u672c\u6846, \u6587\u672c\u57df, \u5bc6\u7801 &amp; \u9690\u85cf\u57df<\/strong><br \/>\n\u521b\u5efa\u6587\u672c\u6846<\/p>\n<pre class=\"brush:php\">echo Form::text('username');\r\n<\/pre>\n<p>\u6307\u5b9a\u9ed8\u8ba4\u503c<\/p>\n<pre class=\"brush:php\">echo Form::text('email', 'example@gmail.com');\r\n<\/pre>\n<blockquote><p>Note: hidden \u548c textarea \u65b9\u6cd5\u7684\u53c2\u6570\u548c text \u76f8\u540c.<\/p><\/blockquote>\n<p>\u751f\u6210\u5bc6\u7801\u8f93\u5165\u6846<\/p>\n<pre class=\"brush:php\">echo Form::password('password', array('class' =&gt; 'awesome'));\r\n<\/pre>\n<p>\u751f\u6210\u5176\u4ed6\u8f93\u5165\u6846<\/p>\n<pre class=\"brush:php\">echo Form::email($name, $value = null, $attributes = array());\r\necho Form::file($name, $attributes = array());\r\n<\/pre>\n<p><strong>\u591a\u9009\u548c\u5355\u9009<\/strong><br \/>\n\u751f\u6210\u5355\u9009\u548c\u591a\u9009<\/p>\n<pre class=\"brush:php\">echo Form::checkbox('name', 'value');\r\n\r\necho Form::radio('name', 'value');\r\n<\/pre>\n<p>\u751f\u6210\u5e26\u6709\u9009\u4e2d\u72b6\u6001\u7684\u8868\u5355\u5143\u7d20<\/p>\n<pre class=\"brush:php\">echo Form::checkbox('name', 'value', true);\r\n\r\necho Form::radio('name', 'value', true);\r\n<\/pre>\n<p><strong>\u6570\u5b57<\/strong><br \/>\n\u751f\u6210\u6570\u5b57\u8f93\u5165\u6846<\/p>\n<pre class=\"brush:php\">echo Form::number('name', 'value');\r\n<\/pre>\n<p><strong>\u65e5\u671f<\/strong><br \/>\n\u751f\u6210\u65e5\u671f\u8f93\u5165\u6846<\/p>\n<pre class=\"brush:php\">echo Form::date('name', \\Carbon\\Carbon::now());\r\n<\/pre>\n<p><strong>\u6587\u4ef6\u9009\u62e9\u5668<\/strong><br \/>\n\u751f\u6210\u6587\u4ef6\u9009\u62e9\u5668<\/p>\n<pre class=\"brush:php\">echo Form::file('image');\r\n<\/pre>\n<blockquote><p>Note: \u8868\u5355\u4e2d\u5fc5\u987b\u8bbe\u7f6e files \u53c2\u6570\u7684\u503c\u4e3a true<\/p><\/blockquote>\n<p><strong>\u4e0b\u62c9\u5217\u8868<\/strong><br \/>\n\u751f\u6210\u4e0b\u62c9\u5217\u8868<\/p>\n<pre class=\"brush:php\">echo Form::select('size', array('L' =&gt; 'Large', 'S' =&gt; 'Small'));\r\n<\/pre>\n<p>\u751f\u6210\u6709\u9ed8\u8ba4\u503c\u7684\u4e0b\u62c9\u5217\u8868<\/p>\n<pre class=\"brush:php\">echo Form::select('size', array('L' =&gt; 'Large', 'S' =&gt; 'Small'), 'S');\r\n<\/pre>\n<p>\u751f\u6210\u7a7a\u5360\u4f4d\u7b26\u7684 \u4e0b\u62c9\u5217\u8868<br \/>\n\u8fd9\u56de\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u4efb\u4f55\u503c\u7684\u5143\u7d20\u4f5c\u4e3a\u4e0b\u62c9\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u9009\u62e9\u503c.<\/p>\n<pre class=\"brush:php\">echo Form::select('size', array('L' =&gt; 'Large', 'S' =&gt; 'Small'), null, ['placeholder' =&gt; 'Pick a size...']);\r\n<\/pre>\n<p>\u751f\u6210\u5206\u7ec4\u7684\u5217\u8868<\/p>\n<pre class=\"brush:php\">echo Form::select('animal', array(\r\n'Cats' =&gt; array('leopard' =&gt; 'Leopard'),\r\n'Dogs' =&gt; array('spaniel' =&gt; 'Spaniel'),\r\n));\r\n<\/pre>\n<p>\u751f\u6210\u8303\u56f4\u9009\u62e9\u503c\u7684\u4e0b\u62c9\u5217\u8868<\/p>\n<pre class=\"brush:php\">echo Form::selectRange('number', 10, 20);\r\n<\/pre>\n<p>\u751f\u6210\u6709\u6708\u4efd\u540d\u79f0\u7684\u9009\u62e9\u503c<\/p>\n<pre class=\"brush:php\">echo Form::selectMonth('month');\r\n<\/pre>\n<p><strong>\u6309\u94ae<\/strong><br \/>\n\u751f\u6210\u63d0\u4ea4\u6309\u94ae<\/p>\n<pre class=\"brush:php\">echo Form::submit('Click Me!');\r\n<\/pre>\n<blockquote><p>Note: \u60f3\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u5143\u7d20? \u8bd5\u7528 button \u65b9\u6cd5. \u4ed6\u548c submit \u65b9\u6cd5\u6709\u76f8\u540c\u7684\u53c2\u6570.<\/p><\/blockquote>\n<p><strong>\u81ea\u5b9a\u4e49\u8868\u5355\u5143\u7d20<\/strong><br \/>\n\u6ce8\u518c\u4e00\u4e2a\u65b0\u7684\u8868\u5355\u5143\u7d20<br \/>\n\u7528\u6765\u5f88\u65b9\u4fbf\u7684\u6765\u81ea\u5b9a\u4e49\u4e00\u4e2a\u8868\u5355\u5143\u7d20\u7684\u65b9\u6cd5\u53eb\u505a macros . \u5408\u7406\u662f\u600e\u6837\u4f7f\u7528\u5b83. \u9996\u5148\u7b80\u5355\u7684\u4f7f\u7528\u540d\u79f0\u548c\u95ed\u5305\u51fd\u6570\u6765\u6ce8\u518c\u4e00\u4e2a :<\/p>\n<pre class=\"brush:php\">Form::macro('myField', function()\r\n{\r\nreturn '<input type=\"awesome\" \/>';\r\n});\r\n<\/pre>\n<p>\u73b0\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u540d\u5b57\u6765\u8c03\u7528\u8fd9\u4e2a macro<\/p>\n<p><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u7684 Form Macro<\/strong><\/p>\n<pre class=\"brush:php\">echo Form::myField();\r\n<\/pre>\n<p><strong>\u751f\u6210URL<\/strong><br \/>\nlink_to<br \/>\n\u6839\u636e\u7ed9\u5b9a\u7684URL\u751f\u6210 html \u94fe\u63a5<\/p>\n<pre class=\"brush:php\">echo link_to('foo\/bar', $title = null, $attributes = array(), $secure = null);\r\n<\/pre>\n<p>link_to_asset<br \/>\n\u751f\u6210\u4e00\u4e2a\u94fe\u63a5\u5230\u6307\u5b9a\u8d44\u6e90\u7684 html<\/p>\n<pre class=\"brush:php\">echo link_to_asset('foo\/bar.zip', $title = null, $attributes = array(), $secure = null);\r\n<\/pre>\n<p>link_to_route<br \/>\n\u751f\u6210\u4e00\u4e2a\u6839\u636e\u7ed9\u5b9a\u8def\u7531\u7684html\u94fe\u63a5<\/p>\n<pre class=\"brush:php\">echo link_to_route('route.name', $title = null, $parameters = array(), $attributes = array());\r\n<\/pre>\n<p>link_to_action<br \/>\n\u6839\u636e\u6307\u5b9a\u7684\u63a7\u5236\u5668\/\u65b9\u6cd5\u6765\u751f\u6210 html \u94fe\u63a5<\/p>\n<pre class=\"brush:php\">echo link_to_action('HomeController@getIndex', $title = null, $parameters = array(), $attributes = array());\r\n<\/pre>\n<p>\u76f8\u5173\u94fe\u63a5\uff1ahttps:\/\/laravelcollective.com\/docs\/5.1\/html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>illuminate\/html \u4f5c\u4e3a\u4ece 4.* \u7248\u672c\u4e2d\u5206\u652f\u51fa\u6765\u7684\u5bf9\u4e8e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,15],"tags":[180,140,167,36],"class_list":["post-1084","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-Note","category-Php","category-Program","tag-form","tag-html","tag-laravel","tag-php-2"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1084"}],"version-history":[{"count":3,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1084\/revisions"}],"predecessor-version":[{"id":1089,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1084\/revisions\/1089"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/media\/1086"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}