如何在 Laravel 中使用基本路由方法?

use*_*399 3 html php routes laravel laravel-5

我从 Laravel 文档中获得了一些文档。但我无法从中清楚地获得详细信息。有很多路由方法以及如何使用它们来满足我的要求?通常大多数人都在使用它,但其他路由方法是什么?

Route::get()
Route::post()
Run Code Online (Sandbox Code Playgroud)

如何通过此路由传递消息或值?像这样使用控制器是唯一的方法吗?

Route::get('/app', 'AppController@index');
Run Code Online (Sandbox Code Playgroud)

K.S*_*gar 5

Laravel 中的路由类型

Laravel 中有一些路由方法,有

1. 基本 GET 路由

GET是用于检索资源的方法。在这个例子中,我们只是简单地获取用户路由需求,然后将消息返回给他。

Route::get('/home', function() { return 'This is Home'; });
Run Code Online (Sandbox Code Playgroud)

2. 基本POST路由

要发出POST请求,您可以简单地使用 post(); 方法,这意味着当您使用 提交表单时action="myForm" method="POST",您希望POST使用此POST路由捕获响应。

Route::post('/myForm', function() {return 'Your Form has posted '; });
Run Code Online (Sandbox Code Playgroud)

3. 为多个动词注册一个路由

在这里,您可以在一条路线中检索GET请求和POST请求。MATCH将在这里收到该请求,

Route::match(array('GET', 'POST'), '/home', function() { return 'GET & POST'; }); 
Run Code Online (Sandbox Code Playgroud)

4. 任何 HTTP 动词

注册响应任何 HTTP 动词的路由。这将根据参数捕获来自您的 URL 的所有请求。

Route::any('/home', function() {  return 'Hello World'; });
Run Code Online (Sandbox Code Playgroud)

Laravel 中路由的使用

当您使用 时Route::,您可以在此处管理您的控制器功能和视图,如下所示,

1.简单的消息返回

您可以返回一条简单的消息,当用户请求该 URL 时,该消息将显示在网页中。

Route::get('/home', function(){return 'You Are Requesting Home';});
Run Code Online (Sandbox Code Playgroud)

2. 返回视图

您可以返回一个视图,当用户请求该 URL 时,该视图将显示在网页中

// show a static view for the home page (app/views/home.blade.php)
Route::get('/home', function()
{
    return View::make('home');
});
Run Code Online (Sandbox Code Playgroud)

3. 请求控制器功能

当用户请求该 URL 时,您可以从控制器调用函数

// call a index function from HomeController (app/Http/Controllers)
Route::get('/home', 'HomeController@index');
Run Code Online (Sandbox Code Playgroud)

4. 从 URL 中获取一个值

您可以从请求的 URL 中捕获一个值,然后将该值传递给 Controller 中的函数。示例:如果您调用,public/home/1452则值 1452 将被缓存并传递给控制器

// call a show function from HomeController (app/Http/Controllers)
Route::get('/home/{id}', 'HomeController@show');
Run Code Online (Sandbox Code Playgroud)