search
HomeBackend DevelopmentPHP TutorialLaravel 514 + Bootstrap 334 Note 2: Laravel Routing

1 Routing mechanism

Routing in MVC is a very important function. Its function is:
A. Match the incoming request and the parameters attached to the request according to the user access (URL);
B. Call Request to map the Controller's Action method and pass in the parameters;
C. Return the Action method processing result;
The following figure represents a user request in a simple form:


2 In Laravel Routing

In Laravel 5.1.4, the routing configuration file is app/Http/routes.php.

2.1 Directly return string routing

Append the following code segment after the original code:

Route::get('/hw', function () {
    return 'Hello World';
});
Open the browser to access: http://localhost:801/hw, as shown below:


2.2 Route to return the view

Add the following code segment to the above code:

Route::get('/home', function () {
    return view('home');
});
Create the view file: home.php in the directory resources/views, with the following content:

<h1 id="home">home</h1>

Open the browser and visit: http://localhost:801/home


What if the code in the above example needs to pass parameters to the view page? Modify our routing code:

Route::get('/home', function () {
    return view('home', ['name' => '张三']);
});
Modify the view code:

[<?php echo $name; ?>],您好!

Look at the effect of the visit again:

If there are too many views, they are usually stored by module or even by function. Create a new directory in the resources/views directory: public/demo, and then move home.php to this directory.

Modify the routing code in the above example to:

Route::get('/home', function () {
    return view('public.demo.home', ['name' => '张三']);
});
You can still open the page normally when you visit again.

2.3 Routing Parameters

As mentioned before, routing can match the user’s request parameters, so how to match? Append the code snippet to the routing file in the above example:

Route::get('user/{name}', function($name) {
    return '用户姓名:'.$name;
});
Open the browser and visit: http://localhost:801/user/jack

What if there are two parameters? Woolen cloth? Modify the routing code:

Route::get('user/{name}/{age}', function($name,$age) {
    return '用户姓名:'.$name.',年龄:'.$age;
});
Open the browser and visit: http://localhost:801/user/jack/23


What if the age parameter is not necessary? Modify the routing code again:

Route::get('user/{name}/{age?}', function($name,$age=null) {
    return '用户姓名:'.$name.',年龄:'.$age;
});
Access address: http://localhost:801/user/jack

2.4 Constraints on routing parameters

Under normal circumstances, some parameters accessed by users have certain Rules, for example, the user ID when reading user information may be a number, the news ID when modifying news information may be a GUID, etc.

Modify the routes.php file and add the following code:

Route::get('new/{id}', function($id)
{
    return '新闻ID:'.$id;
})->where('id', '[0-9]+');
Open the browser to access http://localhost:801/new/3 like this:


When you can access http://localhost:801/new/abc, the provided page does not exist:


Correspondingly, when multiple parameters are restricted at the same time, you need to use an array , modify the routing code of the above example:

Route::get('new/{id}/{title}', function($id,$title)
{
    return '新闻ID:'.$id.',新闻标题:'.$title;
})->where(['id' => '[0-9]+', 'title' => '[a-z]+']);
The access effect will not be demonstrated here.
In addition, we can configure global restrictions, open the file: app/Providers/RouteServiceProviders.php, and modify the boot method as follows:
    public function boot(Router $router)
    {
        //
        $router->pattern('id', '[0-9]+');
        parent::boot($router);
    }

Modify the routing code in the above example to:

Route::get('new/{id}', function($id)
{
    return '新闻ID:'.$id;
});
The access effect is the same as the above example and will not be demonstrated here.

2.5 Get routing parameters

You can get routing parameters in routes.php to do other operations. Modify the routing code in the above example:

Route::get('new/{id}', function(Request $request, $id)
{
    if ($request->route('id') == '2')
    {
        return '新闻ID是2';
    }else{
        return '新闻ID不是2,值是:'.$id;
    }
});

in routes.php Insert a new line after php:

use Illuminate\Http\Request;

Open the browser and visit http://localhost:801/new/2 and http://localhost:801/new/3 respectively You can see different page effects.


Routing still has many complex functions that need to be studied.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces Laravel 514 + Bootstrap 334 Note 2: Laravel routing, including aspects of 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
mysql为什么连接不上localhostmysql为什么连接不上localhostAug 10, 2023 pm 02:55 PM

mysql连接不上localhost的原因有mysql服务未启动、mysql端口被占用和MySQL配置文件问题。详细介绍:1、在Windows系统中,可以通过在命令提示符下输入“services.msc”来打开服务管理器,然后找到mysql服务,确保其状态为“运行中”。在Linux系统中,可以使用“services.msc”命令来检查和控制服务状态;2、可以通过打开命令等等。

C语言return的用法详解C语言return的用法详解Oct 07, 2023 am 10:58 AM

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

function是什么意思function是什么意思Aug 04, 2023 am 10:33 AM

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

localhost打不开怎么办localhost打不开怎么办Nov 07, 2023 pm 02:47 PM

解决办法:1、检查服务器的运行状态,并确保它正在监听正确的端口;2、尝试暂时禁用防火墙或安全软件,然后重新尝试访问localhost;3、检查操作系统的hosts文件,确保localhost的解析正确;4、尝试重启网络适配器或重新配置网络连接;5、尝试更改本地服务器使用的端口,或关闭其他占用相同端口的程序;6、尝试在hosts文件中手动添加对应的IP地址和域名等等。

Java中return和finally语句的执行顺序是怎样的?Java中return和finally语句的执行顺序是怎样的?Apr 25, 2023 pm 07:55 PM

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

linux localhost是什么意思linux localhost是什么意思Mar 14, 2023 am 09:53 AM

linux localhost的意思是“计算机主机名”,主机名用于在网络上识别独立的计算机;在“root@localhost”中的root代表当前登录的用户,在Linux中管理员账户是root,用户以root身份登录到linux本机。

"enumerate()"函数在Python中的用途是什么?"enumerate()"函数在Python中的用途是什么?Sep 01, 2023 am 11:29 AM

在本文中,我们将了解enumerate()函数以及Python中“enumerate()”函数的用途。什么是enumerate()函数?Python的enumerate()函数接受数据集合作为参数并返回一个枚举对象。枚举对象以键值对的形式返回。key是每个item对应的索引,value是items。语法enumerate(iterable,start)参数iterable-传入的数据集合可以作为枚举对象返回,称为iterablestart-顾名思义,枚举对象的起始索引由start定义。如果我们忽

MySQL.proc表的作用和功能详解MySQL.proc表的作用和功能详解Mar 16, 2024 am 09:03 AM

MySQL.proc表的作用和功能详解MySQL是一种流行的关系型数据库管理系统,开发者在使用MySQL时常常会涉及到存储过程(StoredProcedure)的创建和管理。而MySQL.proc表则是一个非常重要的系统表,它存储了数据库中所有的存储过程的相关信息,包括存储过程的名称、定义、参数等。在本文中,我们将详细解释MySQL.proc表的作用和功能

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools