Table of Contents
[Laravel] Laravel的基本使用,laravel使用
Home php教程 php手册 [Laravel] Laravel的基本使用,laravel使用

[Laravel] Laravel的基本使用,laravel使用

Jun 13, 2016 am 08:42 AM
g http laravel use Basic accomplish of routing

[Laravel] Laravel的基本使用,laravel使用

[Laravel] Laravel的基本HTTP路由

 

使用Laravel的基本路由,实现get请求响应,找到文件app/Http/routes.php

调用Route的静态方法get(),实现get响应,参数:string类型的路径,匿名函数function(){}

匿名函数内部,返回string数据

 

实现post,put,delete的请求,同上

 

实现get传递参数的路由,调用Route的静态方法get(),参数:路径,匿名函数

路径,大括号包裹参数名,不含$,例如:’/user/{id}’

匿名函数,接收参数,例如:function($id){}

 

[Laravel] Laraval的基本控制器

 

在app/Http/Controllers目录下,新建一个Index/IndexController.php

定义命名空间,namespace App\Http\Controllers\Index

引入Controller基本控制器,use App\Http\Controllers\Controller

定义IndexController继承Controller

实现方法index,返回数据

定义路由指定控制器的行为,例如:Route::get("/index","Index\IndexController@index");,

注意命名空间部分,新建的控制器是在根命名空间下面,指定的时候添加自己新加的命名空间

 

[Laravel] Laravel的基本视图

在目录resources/views/下面,创建index/index.php

在控制器中使用函数view()来调用模板,参数:文件路径(.分隔目录),数据

 

路由:routes.php

 

<?<span>php

</span><span>/*</span><span>
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
</span><span>*/</span>
<span>/*</span><span>测试get post</span><span>*/</span><span> 
Route::get(</span>'/'<span>, function () {
    $url</span>=url("index"<span>);
    </span><span>return</span> "Hello World"<span>.$url;
    </span><span>//</span><span>return view('welcome');</span>
<span>});
Route::post(</span>"/post"<span>,function(){
    </span><span>return</span> "测试post"<span>;
});

</span><span>/*</span><span>传递参数</span><span>*/</span><span>
Route::get(</span>"/user/{id}"<span>,function($id){
    </span><span>return</span> "用户"<span>.$id;
});
</span><span>/*</span><span>使用控制器</span><span>*/</span><span>
Route::get(</span>"/index","Index\IndexController@index"<span>);
</span><span>/*</span><span>
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
</span><span>*/</span><span>

Route::group([</span>'middleware' => ['web'<span>]], function () {
    </span><span>//
</span>});
Copy after login

控制器:IndexController.php

<?<span>php
namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
</span><span>class</span> IndexController <span>extends</span><span> Controller{
    </span><span>public</span><span> function index(){
        $data</span>=<span>array();
        $data[</span>'title']="Index控制器"<span>;
        </span><span>return</span> view("index.index"<span>,$data);
    }
}</span>
Copy after login

模板:index.php

    <span><</span><span>body</span><span>></span>
        <span><</span><span>div </span><span>class</span><span>="container"</span><span>></span>
            <span><</span><span>div </span><span>class</span><span>="content"</span><span>></span>
                <span><</span><span>div </span><span>class</span><span>="title"</span><span>></span><span><?</span><span>php echo $title;</span><span>?></span><span></</span><span>div</span><span>></span>
            <span></</span><span>div</span><span>></span>
        <span></</span><span>div</span><span>></span>
    <span></</span><span>body</span><span>></span>
Copy after login

 

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)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

BitMEX exchange withdrawal rules and advantages and disadvantages BitMEX exchange withdrawal rules and advantages and disadvantages Feb 21, 2025 pm 10:48 PM

BitMEX exchange currency withdrawal requirements: Two-step verification and identity verification must be completed. The minimum amount of withdrawal varies by currency. The withdrawal process includes logging into the account, entering the withdrawal address, entering the amount and confirming transactions. The advantages of BitMEX withdrawal include fast processing, low handling fees, multiple currency support and strict security measures. However, it also faces shortcomings such as insufficient supervision, risk of hacker attacks, restrictions on withdrawals and account freezes.

gate official website entrance gate exchange official website gate official website entrance gate exchange official website Feb 19, 2025 pm 03:03 PM

The official website of Gate.io can be accessed by clicking on the link or entering the URL in the browser. It is recommended to add the URL to a bookmark or favorite for easy access. If you encounter inaccessible issues, try clearing the browser's cache and cookies. Be careful to prevent phishing, the official website of Gate.io will not take the initiative to ask for personal information. In addition, Gate.io provides mobile applications that can be found through the app provider

The latest top ten digital currency trading app rankings in 2025 The latest top ten digital currency trading app rankings in 2025 Feb 27, 2025 pm 06:24 PM

The top ten digital currency trading apps in 2025 are: Binance, OKX, Gate.io, Bitget, Huobi, KuCoin, BitMart, Bybit, Bitfinex, and Poloniex. These transactions stand out in the market for their trading volume, asset selection, leveraged trading, social trading, compliance, ease of use, low transaction fees and fund security.

What is Bitget Launchpool? How to use Bitget Launchpool? What is Bitget Launchpool? How to use Bitget Launchpool? Jun 07, 2024 pm 12:06 PM

BitgetLaunchpool is a dynamic platform designed for all cryptocurrency enthusiasts. BitgetLaunchpool stands out with its unique offering. Here, you can stake your tokens to unlock more rewards, including airdrops, high returns, and a generous prize pool exclusive to early participants. What is BitgetLaunchpool? BitgetLaunchpool is a cryptocurrency platform where tokens can be staked and earned with user-friendly terms and conditions. By investing BGB or other tokens in Launchpool, users have the opportunity to receive free airdrops, earnings and participate in generous bonus pools. The income from pledged assets is calculated within T+1 hours, and the rewards are based on

How to trade in TREZOR and what to note How to trade in TREZOR and what to note Feb 21, 2025 pm 10:30 PM

Transactions on TREZOR Connect the TREZOR device and install the TREZORBridge application. Open the TREZOR suite and select the transaction type (send, receive, or redeemed). Enter transaction details (address, amount, fee, etc.). Check the details and press Confirm on the TREZOR device to authorize the transaction.

gate.io sesame door download Chinese tutorial gate.io sesame door download Chinese tutorial Feb 28, 2025 am 10:54 AM

This article will guide you in detail how to access the official website of Gate.io, switch Chinese language, register or log in to your account, as well as optional mobile app download and use procedures, helping you easily get started with the Gate.io exchange. For more tutorials on using Gate.io in Chinese, please continue reading.

See all articles