带参数的 Laravel 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42359582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:22:59  来源:igfitidea点击:

Laravel route with parameters

laravelroutes

提问by Prit

Am using laravel route for approving some form from email. So i have encrypted some variables and created link which is like

我正在使用 laravel 路线来批准电子邮件中的某些表格。所以我加密了一些变量并创建了链接,就像

<a href="http://localhost/travel/tr/approveRequest?id=<?=$Encoded_travelRq_id?>&gsID<?=$Encoded_emp_gslab_id?>&decrypt<?=$Encoded_iv?>/">Approve</a>;

now how route can be written for this on laravel side in which i can seperate the variables like id, gsID, decrypt from url and can send to controller's function used against that route?

现在如何在 Laravel 端为此编写路由,我可以在其中分离诸如 id、gsID、从 url 解密之类的变量,并可以发送到针对该路由使用的控制器函数?

回答by EddyTheDove

Simply write a GETroute to approveRequest:

简单地写一个GET路由到approveRequest

Route::get('approveRequest', 'ApproveController@approve');

Because you are using URL parameters, you can simply get them in the approve()function like this

因为您使用的是 URL 参数,所以您可以approve()像这样简单地在函数中获取它们

public function approve(Request $request) 
{
     $id = $request->id;
     $gsID = $request->get('gsID');
     .... and so on for all your variables.
}

With this approach the order of parameters does not matter.

使用这种方法,参数的顺序无关紧要。

回答by Caio César Brito

Usually I use two ways:

通常我使用两种方式:

1o way:

1o方式:

The route:

路线:

Route::get('approveRequest', 'ApproveController@approve');

The controller:

控制器:

public function approve (Request $request) {
    $var1 = $request->input('var1');
    $var2 = $request->input('var2');

    (...) do something with $var1 and $var2
}

2o way:

2o方式:

The route:

路线:

Route::get('approveRequest/{var1}/{var2}', 'ApproveController@approve');

The controller:

控制器:

public function approve ($var1, $var2) {

    (...) do something with $var1 and $var2: they already have a instance
}

Good Luck! I hope be useful.

祝你好运!我希望有用。

回答by Sagar Arora

There are many methods to do this.

有很多方法可以做到这一点。

Method 1

方法一

If you make the url like below:

如果你像下面这样制作网址:

<a href="http://localhost/travel/tr/approveRequest/<?=$Encoded_travelRq_id?>/<?=$Encoded_emp_gslab_id?>/<?=$Encoded_iv?>">Approve</a>;

You can make route with parameters as like below.

您可以使用如下参数创建路由。

 Route::get('approveRequest/{par1}/{par2}/{par3}', 'ApproveController@approve');

And in your function get the respective parameter as following:

并在您的函数中获取相应的参数如下:

public function approve($par1,$par2,$par3,) 
{
     $id = $par1;
     $gsID = $par2;
     .... and so on for all your variables.
 }

Method 2 use request method:

方法二使用请求方法:

If your url is like:

如果你的网址是这样的:

  <a href="http://localhost/travel/tr/approveRequest?id=<?=$Encoded_travelRq_id?>&gsID<?=$Encoded_emp_gslab_id?>&decrypt<?=$Encoded_iv?>/">Approve</a>;

Then define Route Like:

然后定义路由喜欢:

   Route::post('approveRequest', 'ApproveController@approve');

Then in your controller function you get parameters as below:

然后在您的控制器功能中,您将获得如下参数:

Use Request after the namespace in youe controller class

在您的控制器类中的命名空间之后使用 Request

            namespace App\Http\Controllers;
            use Illuminate\Http\Request;

            class SomeClassController extends Controller {

                public function approve(Request $request) 
               {
                 $id = $request->id;
                 $gsID = $request->get('gsID');
                 .... and so on for all your variables.
                }
             }