Laravel controller: Difference between revisions
From AWVVO
Jump to navigationJump to search
(One intermediate revision by the same user not shown) | |||
Line 9: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Laravel]] | |||
== Get request parameters == | == Get request parameters == | ||
Line 24: | Line 26: | ||
// Access query string parameters specifically | // Access query string parameters specifically | ||
$queryParam = $request->query('key', 'default_value'); | $queryParam = $request->query('key', 'default_value'); | ||
} | |||
</syntaxhighlight> | |||
== Get headers == | |||
<syntaxhighlight lang="php" copy> | |||
use Illuminate\Http\Request; | |||
public function index(Request $request) | |||
{ | |||
// Get a specific header | |||
$authHeader = $request->header('Authorization'); | |||
// Get all headers as an array | |||
$headers = $request->headers->all(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 13:57, 19 January 2025
action template
public function index()
{
$data = [];
return response()->json($data);
}
Get request parameters
use Illuminate\Http\Request;
public function index(Request $request)
{
// Get a specific parameter (with default value if not present)
$param = $request->input('key', 'default_value');
// Get all parameters as an array
$allParams = $request->all();
// Access query string parameters specifically
$queryParam = $request->query('key', 'default_value');
}
Get headers
use Illuminate\Http\Request;
public function index(Request $request)
{
// Get a specific header
$authHeader = $request->header('Authorization');
// Get all headers as an array
$headers = $request->headers->all();
}