public function selectService(Request $request) { // Validate service selection…
- User
public function selectService(Request request) { // Validate service selection validator = Validator::make($request->all(), [ 'service_id' => 'required|exists:services,sid' ]);
codeif ($validator->fails()) { return back()->withErrors($validator); } // Get user data from session $userData = session('user_data'); $selectedService = collect($userData['services']) ->firstWhere('sid', $request->service_id); if ($selectedService) { // Redirect to service domain with authentication token/method return redirect()->to("https://" . $selectedService['domain'] . $selectedService['login'] . "?m=" . $userData['Token']); } return back()->withErrors(['service' => '錯誤選項,請重新登入.']); }Change redirect that can send the data with POST method
- Assistant
To change the redirect to send data via POST instead of GET, you cannot just use
redirect()->to()since HTTP redirects with Laravel or HTTP status codes don't support POST requests directly.Common approach to achieve this behavior is:
- Return a view with a form that has method POST and action to the target URL
- User
Any modification I need to do in routes?
- Assistant
No special modifications to your existing routes are required just to implement this POST "redirect" approach.
Why?
- Your controller method (
selectService) remains the same and is still routed as before (likely via a POST or GET route). - The new Blade view (
post_redirect.blade.php) is not accessed directly via route; it is returned internally by the controller. - The browser renders that view and auto-submits the hidden form to the target URL, which is an external domain or different endpoint.
Summary of what you need in routes:
- Ensure you have a route definition pointing to the
selectServicemethod, e.g.:
php// web.php Route::post('/select-service', [YourController::class, 'selectService'])->name('select.service'); - Your controller method (
- User
I store the post_redirect.blade.php in resources/views/auth, how can I access the file without route
- Assistant
You don’t access Blade view files (like
post_redirect.blade.php) directly via URL or route. Instead, Blade views are rendered and returned by your controller, and the user sees the resulting HTML page.
What this means for your case:
- Your controller method (e.g.,
selectService) returns the Blade view:
phpreturn view('auth.post_redirect', compact('url', 'token')); - Your controller method (e.g.,
- User
how can I avoid the message show by browser when I contain the POST data?
- Assistant
- User
I have the GET value WNudgmlWuTqx7QMskIvM8S4ZGB1BtUjzIn85GDcRBiTVv+z0UWa0/gvsxWfzHOS8 in URL, when I handle with php script, the + in value will be replace as whitespace, why? and How can I get the exact value with + correctley
- Assistant
This happens because in URL query parameters, the plus sign () is a special character that represents a space when decoded by PHP (and many web frameworks).