如何在Laravel中用问号调用apiurl?
How to call api url with question marks in Laravel?
我正在尝试在 Laravel 中调用带有问号的 url...
我正在研究使用以下 url 的计算器 api,例如:
'api/calculator/add?a={a}&b={b}'
如何称呼这样的 URL?
我在api.php中有以下路线:
Route::get('/calculator/add?a={a}&b={b}', function ($a, $b ) {
return Calculator::sub($a, $b);
})
Route::get('/calculator/sub?a={a}&b={b}', function ($a, $b ) {
return Calculator::sub($a, $b);
});
Route::get('/calculator/div?a={a}&b={b}', function ($a, $b ) {
return Calculator::div($a, $b);
});
Route::get('/calculator/mul?a={a}&b={b}', function ($a, $b ) {
return Calculator::mul($a, $b);
});
Route::get('/calculator/mod?a={a}&b={b}', function ($a, $b ) {
return Calculator::mod($a, $b);
});
我还有一个计算器控制器:
use Illuminate\Http\Request;
use App\Calculator;
use App\Http\Requests\CalculatorRequest;
class CalculatorController extends Controller
{
private calculator = new Calculator();
public function add(CalculatorRequest $request)
{
// TODO @laravel-test
echo var_dump($request);
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function sub(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function div(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function mul(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function mod(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
}
我在互联网上找不到任何信息..
我该怎么做?
提前致谢。
据我所知,你不能这样做。
当您执行 url/something?a=123&b=456
时,您可以执行以下操作:
public function show(Request $request)
{
$a = $request->input('a');
$b = $request->input('b');
dd($a, $b); // $a = 123, $b = 456
}
但是你的路线是:
Route::get('url/something', [YourController::class, 'show']);
我正在尝试在 Laravel 中调用带有问号的 url... 我正在研究使用以下 url 的计算器 api,例如:
'api/calculator/add?a={a}&b={b}'
如何称呼这样的 URL?
我在api.php中有以下路线:
Route::get('/calculator/add?a={a}&b={b}', function ($a, $b ) {
return Calculator::sub($a, $b);
})
Route::get('/calculator/sub?a={a}&b={b}', function ($a, $b ) {
return Calculator::sub($a, $b);
});
Route::get('/calculator/div?a={a}&b={b}', function ($a, $b ) {
return Calculator::div($a, $b);
});
Route::get('/calculator/mul?a={a}&b={b}', function ($a, $b ) {
return Calculator::mul($a, $b);
});
Route::get('/calculator/mod?a={a}&b={b}', function ($a, $b ) {
return Calculator::mod($a, $b);
});
我还有一个计算器控制器:
use Illuminate\Http\Request;
use App\Calculator;
use App\Http\Requests\CalculatorRequest;
class CalculatorController extends Controller
{
private calculator = new Calculator();
public function add(CalculatorRequest $request)
{
// TODO @laravel-test
echo var_dump($request);
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function sub(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function div(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function mul(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
public function mod(CalculatorRequest $request)
{
// TODO @laravel-test
$request->validate( [
'a' => ['required', new CalculatorRequest()]
'b' => ['required', new CalculatorRequest()]
]);
// Calculate and return the result...
}
}
我在互联网上找不到任何信息.. 我该怎么做?
提前致谢。
据我所知,你不能这样做。
当您执行 url/something?a=123&b=456
时,您可以执行以下操作:
public function show(Request $request)
{
$a = $request->input('a');
$b = $request->input('b');
dd($a, $b); // $a = 123, $b = 456
}
但是你的路线是:
Route::get('url/something', [YourController::class, 'show']);