多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 基本路由 在学习新框架时,您的首要任务之一是确定如何处理路由。或者换句话说,当我访问浏览器中的首页URL时,浏览器中展现欢迎页面! 所有的 `Laravel` 路由都在 `routes` 目录中的路由文件中定义,这些文件都由框架自动加载。`routes/web.php` 文件用于定义 web 界面的路由。这里面的路由都会被分配给 `web` 中间件组,它提供了会话状态和 `CSRF` 保护等功能。定义在 `routes/api.php` 中的路由都是无状态的,并且被分配了 `api` 中间件组。 今天我们先从基本的web界面路由开始,打开 `routes/web.php` : ```php Route::get('/', function () { return view('welcome'); }); ``` 构建最基本的路由只需要一个 URI 与一个 闭包,这里提供了一个非常简单优雅的定义路由的方法,向浏览器展示 `welcome` 页面信息。 `welcome` 试图在 `resources/views` 文件夹下定义,简单修改一下看是否改变: ```html <!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 84px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { margin-bottom: 30px; } </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title m-b-md"> Hello Laravel </div> <div class="links"> <a href="https://laravel.com/docs">Documentation</a> <a href="https://laracasts.com">Laracasts</a> <a href="https://laravel-news.com">News</a> <a href="https://nova.laravel.com">Nova</a> <a href="https://forge.laravel.com">Forge</a> <a href="https://github.com/laravel/laravel">GitHub</a> </div> </div> </div> </body> </html> ``` ### 基本使用 在web路由页面创建 `about` 和 `news` 路由: ```php Route::get('/about', function () { return view('about'); }); Route::get('/news', function () { return view('news'); }); ``` 在 `resources/views` 下创建 `about.blade.php` 和 `news.blade.php` 试图: * about.blade.php ```html <!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 84px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { margin-bottom: 30px; } </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title m-b-md"> 假装写代码的晚黎 </div> <div class="links"> <a href="/">Home</a> <a href="/about">about</a> <a href="/news">News</a> </div> </div> </div> </body> </html> ``` * news.blade.php ``` <!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 84px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { margin-bottom: 30px; } </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title m-b-md"> Larametrics </div> <p> Larametrics is an open-source self-hosted metrics and notifications platform for Laravel apps created by Andrew Schmelyun. It’s simple to get started and only takes a few minutes to get started. The official Getting Started documentation describes Larametrics as follows: What exactly does that mean? Think of it as a simple version of sentry.io or rollbar.com. Consisting of a series of watchers keeping an eye on model changes, log entries, and route requests, Larametrics alerts you through email or Slack when one of your notification triggers is met. The documentation describes a few notification examples: Email me when a notice or info message is logged Alert me through Slack and email when an error is logged Let me know when an Admin model is created or deleted Tell me when someone visits the /auth/login route </p> <div class="links"> <a href="/">Home</a> <a href="/about">about</a> <a href="/news">News</a> </div> </div> </div> </body> </html> ``` ### 总结 这节简单的介绍了 `Laravel` 框架的基本路由使用,需要在web浏览器中看见我们想要呈现的页面,需要两个步骤: 1. 在 `routes/web.php` 定义web页面路由 2. 在 `resources/views` 下创建对应路由的视图文件