Laravel route with parameters

前端 未结 3 1576
陌清茗
陌清茗 2021-01-18 08:28

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



        
相关标签:
3条回答
  • 2021-01-18 09:05

    Usually I use two ways:

    1º way:

    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
    }
    

    2º way:

    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
    }
    
    0 讨论(0)
  • 2021-01-18 09:08

    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

                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.
                    }
                 }
    
    0 讨论(0)
  • 2021-01-18 09:25

    Simply write a GET route to approveRequest :

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

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

    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.

    0 讨论(0)
提交回复
热议问题