如何在控制器中获取所需的参数?

huangapple go评论74阅读模式
英文:

How to get the required parameters in a controller?

问题

我不确定为什么找不到它,但我需要获取控制器中所需参数的值。例如,如果路由是/route/{param},如果有人输入/route/123,我想要获取123

public function some_method(Request $request)
{
    // 获取 {param} 的值,123
}

我所见到的只是它通常用于路由模型绑定,或者不使用Request(https://laravel.com/docs/10.x/routing#route-model-binding):

use App\Http\Controllers\UserController;
use App\Models\User;

// 路由定义...
Route::get('/users/{user}', [UserController::class, 'show']);

// 控制器方法定义...
public function show(User $user)
{
    return view('user.profile', ['user' => $user]);
}

*但也许我甚至不需要Request $request,因为我可以通过request()助手来获取它?

英文:

I'm not sure why I couldn't find it, but I need to get the value of the required parameter in the controller. For example if the route is /route/{param}, and if someone entered /route/123, I want to get 123:

public function some_method(Request $request)
{
    // get {param} value, 123
}

All I've seen is that it's either used for route-model-binding or that it doesn't use the Request (https://laravel.com/docs/10.x/routing#route-model-binding):

use App\Http\Controllers\UserController;
use App\Models\User;
 
// Route definition...
Route::get('/users/{user}', [UserController::class, 'show']);
 
// Controller method definition...
public function show(User $user)
{
    return view('user.profile', ['user' => $user]);
}

*But perhaps I don't even need Request $request because I can get it via the request() helper?

答案1

得分: 2

你可以同时使用 Request 参数、值参数或模型绑定,Laravel 足够智能以识别您期望的数据类型(因为给定了参数的类型)。因此,您可以这样做:

use App\Http\Controllers\YourController;

// 路由定义...
Route::get('/route/{param}', [YourController::class, 'some_method']);

// 控制器方法定义...
public function some_method(Request $request, string $param)
{
    ... // 对于 /route/123,$param 将具有 '123' 的值
}

如果您要获取的 ID 是某个模型的 ID,那么您可以同时使用 Request 和模型绑定:

use App\Http\Controllers\YourController;
use App\Models\YourModel;

// 路由定义...
Route::get('/route/{param}', [YourController::class, 'some_method']);

// 控制器方法定义...
public function some_method(Request $request, YourModel $param)
{
    ... // 对于 /route/123,您将获得 ID 为 123 的 YourModel
}
英文:

You can have both Request parameter, value parameter or model binding, laravel is smart enough to recognize what's type of data you expect (due to giving type to the parameter). So you can do:

use App\Http\Controllers\YourController;
 
// Route definition...
Route::get('/route/{param}', [YourController::class, 'some_method']);
 
// Controller method definition...
public function some_method(Request $request, string $param)
{
    ... // for /route/123 $param will have '123' value
}

if the id that you want to get is id of some model, then you can have both Request and model binding:

use App\Http\Controllers\YourController;
use App\Models\YourModel;
 
// Route definition...
Route::get('/route/{param}', [YourController::class, 'some_method']);
 
// Controller method definition...
public function some_method(Request $request, YourModel $param)
{
    ... // for /route/123 you will get YourModel with id 123
}

答案2

得分: 0

如果您想从路由中获取必需的参数,这里有一个很好的示例:

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SomeController;

Route::get('/endpoint/{param}', [SomeController::class, 'someMethod']);

SomeController.php

<?php

namespace App\Http\Controllers;

class SomeController extends Controller
{
   public function someMethod($param)
   {
      // 现在您可以使用 dd() 来检查该值
      dd($param);
   }
}

注意: 确保您的路由参数和函数参数具有相同的名称

您还可以使用段来访问参数值,这里是另一个示例:

public function someMethod($param)
{
   request()->segment(2);
}
英文:

If you want to get required parameter from route so here is a good example:

web.php

&lt;?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SomeController;

Route::get(&#39;/endpoint/{param}&#39;, [SomeController::class, &#39;someMethod&#39;]);

SomeController.php

&lt;?php

namespace App\Http\Controllers;

class SomeController extends Controller
{
   public function someMethod($param)
   {
      // Now you can check the value
      // by using dd()
      dd($param);
   }
}

Note: Make sure your route parameter and function parameter must be with same name

You can also access parameter value by using segment, here is another example:

public function someMethod($param)
{
   request()-&gt;segment(2);
}

huangapple
  • 本文由 发表于 2023年7月3日 17:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定