Rumah  >  Artikel  >  pembangunan bahagian belakang  >  laravel陌生知识点快速学习一

laravel陌生知识点快速学习一

WBOY
WBOYasal
2016-08-08 09:19:57806semak imbas

laravel陌生知识点

php参数的默认值

<code><span><span><?php </span><span><span>function</span><span>makecoffee</span><span>(<span>$type</span> = <span>"cappuccino"</span>)</span>
{</span><span>return</span><span>"Making a cup of $type.\n"</span>;
}
<span>echo</span> makecoffee();
<span>echo</span> makecoffee(<span>null</span>);
<span>echo</span> makecoffee(<span>"espresso"</span>);
<span>?></span></span></span></code>

输出

<code>Making <span>a</span> cup <span>of</span> cappuccino.
Making <span>a</span> cup <span>of</span> .
Making <span>a</span> cup <span>of</span> espresso.</code>

模型绑定 (Model Binding)

在RouteServiceProvider中,在boot方法里实现模型绑定

<code><span>public</span> function boot(Router <span>$router</span>)
    {
        <span>parent</span><span>::boot</span>(<span>$router</span>);
        <span>$router</span><span>-></span>model(<span>'users'</span>, <span>'App\User'</span>);
        <span>$router</span><span>-></span>model(<span>'goods'</span>, <span>'App\Good'</span>);
        <span>$router</span><span>-></span>model(<span>'categories'</span>, <span>'App\Category'</span>);
        <span>$router</span><span>-></span>model(<span>'tryClothes'</span>, <span>'App\TryRecord'</span>);
        <span>$router</span><span>-></span>model(<span>'carts'</span>, <span>'App\Cart'</span>);
        <span>$router</span><span>-></span>model(<span>'orders'</span>, <span>'App\Order'</span>);
        <span>$router</span><span>-></span>model(<span>'orderItems'</span>, <span>'App\OrderItem'</span>);
        <span>//</span>
    }</code>

表单申请 (Form Request)

  • 使用以下指令生成自定义Request
<code><span>php</span><span>artisan</span><span>make</span><span>:request</span><span>CreateArticleRequest</span></code>
  • 自定义Request中的方法:authorize() 和 rules();authorize判断是否有权限,rules进行数据验证
<code><span>public</span><span><span>function</span><span>authorize</span><span>()</span>
    {</span><span>return</span><span>true</span>;
    }</code>
<code><span>public</span><span><span>function</span><span>rules</span><span>()</span>
{</span><span>return</span> [
        <span>'title'</span> => <span>'required|min:3'</span>,
        <span>'body'</span> => <span>'required'</span>,
        <span>'published_at'</span> => <span>'required|date'</span>,
        <span>// 也可以使用数组</span><span>//'published_at' => ['required', 'date'],</span>
    ];
}</code>
  • 使用Request的方法中通常是传入了POST数据,之所以定义自定义Request类是为了复用代码与解除耦合性,完全可以使用Validate类进行自定义Request中rules方法的处理
<code><span>public</span><span><span>function</span><span>store</span><span>(Request <span>$request</span>)</span>{</span><span>$this</span>->validate(<span>$request</span>, [<span>'title'</span> => <span>'required|min:3'</span>, <span>'body'</span> =><span>'required'</span>, <span>'published_at'</span> => <span>'required|date'</span>]);
        Article::create(<span>$request</span>->all());
        <span>return</span> redirect(<span>'articles'</span>);
    }</code>
  • 如果通过验证,可以使用$request->all()直接将数据给相关的类
<code>Article<span>::create</span>(<span>$request</span><span>-></span><span>all</span>());</code>

php storm laravel 代码提示

  • https://gist.githubusercontent.com/barryvdh/5227822/raw/811f21a14875887635bb3733aef32da51fa0501e/_ide_helper.php
  • 记得在.gitignore文件中添加这个文件

在特定文件夹中创建控制器

<code>php artisan <span>make</span>:controller Console/ConsoleController</code>
  • 注意在routes.php事先写的代码没有问题,否则出现了以下错误
<code><span>[</span>ReflectionException<span>]</span>
  Class App<span>\Http</span><span>\Controllers</span><span>\console</span> does not exist</code>

参考资料

  • Laravel 5.0 - Form Requests
  • http://9iphp.com/web/laravel/laravel-5-form-request-controller-validation.html
  • laracast

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了laravel陌生知识点快速学习一,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP中的include和requireArtikel seterusnya:(二)PHP面向对象理论2