Home  >  Article  >  Backend Development  >  Quickly learn unfamiliar knowledge points in laravel

Quickly learn unfamiliar knowledge points in laravel

WBOY
WBOYOriginal
2016-08-08 09:19:57808browse

Laravel unfamiliar knowledge points

Default values ​​of php parameters

<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></code>

Output

<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 (Model Binding)

In RouteServiceProvider, implement model binding in the boot method

<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 (Form Request)

  • Use the following instructions to generate a custom Request
<code><span>php</span><span>artisan</span><span>make</span><span>:request</span><span>CreateArticleRequest</span></code>
  • Customize the methods in Request: authorize() and rules(); authorize determines whether there is permission, and rules performs data verification
<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>
  • Use the Request method Usually POST data is passed in. The reason why the custom Request class is defined is to reuse code and decouple. You can use the Validate class to process the rules method in the custom Request
<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>
  • If it passes the verification, you can use $request->all() directly sends data to the relevant class
<code>Article<span>::create</span>(<span>$request</span><span>-></span><span>all</span>());</code>

php storm laravel code tips

  • https://gist.githubusercontent.com/barryvdh/5227822/raw/811f21a14875887635bb3733aef32da51fa0501e/_ide_helper. php
  • Remember to add this file in the .gitignore file

Create the controller in a specific folder

<code>php artisan <span>make</span>:controller Console/ConsoleController</code>
  • Note that there is no problem with the code written in routes.php, otherwise the following error will occur
<code><span>[</span>ReflectionException<span>]</span>
  Class App<span>\Http</span><span>\Controllers</span><span>\console</span> does not exist</code>

References

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

Copyright statement: This article is an original article by the blogger, No reproduction is allowed without the permission of the blogger.

The above introduces the quick learning of unfamiliar knowledge points in Laravel, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn