【问题标题】:How to use Basic Routing methods in Laravel?如何在 Laravel 中使用基本路由方法?
【发布时间】:2017-05-14 12:15:59
【问题描述】:

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

Route::get()
Route::post()

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

Route::get('/app', 'AppController@index');

【问题讨论】:

    标签: php html laravel laravel-5 routes


    【解决方案1】:

    Laravel 中的路由类型

    Laravel中有一些路由方法,有

    1.基本 GET 路由

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

    Route::get('/home', function() { return 'This is Home'; });
    

    2。基本 POST 路线

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

    Route::post('/myForm', function() {return 'Your Form has posted '; });
    

    3.为多个动词注册路由

    在这里,您可以在一个路由中检索GET 请求和POST 请求。 MATCH 将在此处收到该请求,

    Route::match(array('GET', 'POST'), '/home', function() { return 'GET & POST'; }); 
    

    4.任何 HTTP 动词

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

    Route::any('/home', function() {  return 'Hello World'; });
    

    Laravel 中路由的使用

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

    1.简单的消息返回

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

    Route::get('/home', function(){return 'You Are Requesting Home';});
    

    2。返回视图

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

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

    3.请求控制器功能

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

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

    4.从 URL 中获取值

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

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

    【讨论】:

    • 谢谢你,我从你的单一回答中得到了更多的东西
    【解决方案2】:

    您可以从Laravel获得有关路由的帮助。

    你必须知道的4种表单数据发送方法——

    1. Route::get<form method="GET">
    2. Route::post<form method="POST">
    3. Route::put for <form method="PUT"> -- 这个是用来更新你的数据库的,我推荐你使用laravelcollective/html,像这样 -- {!! Form::open(['method' => 'PUT']) !!},但是在你的浏览器中你可以找到POST only
    4. Route::delete for <form method="DELETE"> -- 这个是删除数据库中的一个字段,我推荐你使用laravelcollective/html,像这样 -- {!! Form::open(['method' => 'DELETE']) !!},但是在你的网络浏览器中你可以找到@987654335的方法仅限@

    关于 Laravel 路由,你需要了解很多,比如 CRUD 等。

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 2014-10-24
      • 2012-09-14
      • 2016-09-23
      • 2017-10-22
      • 1970-01-01
      • 2014-05-04
      • 2014-02-10
      • 1970-01-01
      相关资源
      最近更新 更多