Home Backend Development PHP Tutorial Quickly learn unfamiliar knowledge points in laravel

Quickly learn unfamiliar knowledge points in laravel

Aug 08, 2016 am 09:19 AM
gt model request router

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login
  • 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>
Copy after login
<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>
Copy after login
  • 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>
Copy after login
  • 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>
Copy after login

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>
Copy after login
  • 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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x How to use the urllib.request.urlopen() function to send a GET request in Python 3.x Jul 30, 2023 am 11:28 AM

How to use the urllib.request.urlopen() function in Python3.x to send a GET request. In network programming, we often need to obtain data from a remote server by sending an HTTP request. In Python, we can use the urllib.request.urlopen() function in the urllib module to send an HTTP request and get the response returned by the server. This article will introduce how to use

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Trezor Cold Wallet: Model One and Model T Features and Usage Guide Trezor Cold Wallet: Model One and Model T Features and Usage Guide Jan 19, 2024 pm 04:12 PM

After problems occurred in many centralized exchanges, more and more cryptocurrency investors began to transfer assets to cold wallets to reduce the risks posed by centralized exchanges. This article will introduce Trezor, the world's earliest cold wallet provider. Since the first cold wallet was launched in 2014, it has been sold in many countries around the world. Trezor's products include Model One launched in 2014 and the advanced version Model T launched in 2018. The following will continue to introduce the differences between these two products and other cold wallets. What is Trezor cold wallet? In 2014, Trezor launched the first cold wallet ModelOne. In addition to common BTC, ETH, USDT and other currencies, the wallet also supports more than 1,000 other currencies.

See all articles