简体   繁体   English

重定向到外部 URL,并在 Laravel 中返​​回

[英]Redirect to external URL with return in laravel

I am trying to send one time password to a user using SMS INDIA HUB API.我正在尝试使用 SMS INDIA HUB API 向用户发送一次性密码。 For that purpose I need to redirect to a URL format:为此,我需要重定向到 URL 格式:

http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898xxxxxx&sid=SenderId&msg=test%20message&fl=0&gwid=2 http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898xxxxxx&sid=SenderId&msg=test%20message&fl=0&gwid=2

If we load this URL, it will return some message.如果我们加载这个 URL,它会返回一些消息。 I need to get that message to.我需要收到这条消息。

I tried like this我试过这样

$url = "http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=wwww&password=eee&msisdn=9197xxxxx&sid=yyyyy&msg=rrrrr&fl=0&gwid=2";

return Redirect::intended($url);

But it is not directing to that link.但它不是指向那个链接。 It tries to load that URL in localhost.它尝试在本地主机中加载该 URL。

Or is there any plugin to send sms using SMS INDIA HUB?或者是否有任何插件可以使用 SMS INDIA HUB 发送短信?

Can anyone help??有人可以帮忙吗??

You should be able to redirect to the url like this您应该能够像这样重定向到网址

return Redirect::to($url);

You can read about Redirects in the Laravel docs here.你可以在这里阅读 Laravel 文档中的重定向。

For Laravel 5.x and above对于 Laravel 5.x 及更高版本

return redirect()->away('https://www.google.com');

as stated in the docs :文档中所述:

Sometimes you may need to redirect to a domain outside of your application.有时您可能需要重定向到应用程序之外的域。 You may do so by calling the away method, which creates a RedirectResponse without any additional URL encoding, validation, or verification:您可以通过调用 away 方法来实现,该方法创建 RedirectResponse 而不需要任何额外的 URL 编码、验证或验证:

Define the url you want to redirect in $url$url定义要重定向的$url

Then just use然后只需使用

return Redirect::away($url);

If you want to redirect inside your views use如果你想在你的视图中重定向使用

return Redirect::to($url);

Read more about Redirect here在此处阅读有关重定向的更多信息

Update 1 :更新 1:

Here is the simple example这是一个简单的例子

return Redirect::to('http://www.google.com');

Update 2 :更新2:

As the Questioner wants to return in the same page由于发问者希望返回同一页面

$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;

您可以使用Redirect::away($url)

对于 Laravel 5.x,我们可以只重定向

return redirect()->to($url);

Also, adding class另外,添加类

      use Illuminate\Http\RedirectResponse;

and after, like this:之后,像这样:

 public function show($id){

    $link = Link::findOrFail($id);  // get data from db table Links

    return new RedirectResponse($link->url);  // and this my external link, 
 }

or -或者 -

 return  new RedirectResponse("http://www.google.com?andParams=yourParams"); 

For external links have to be used full URL string with 'http' in begin.对于外部链接,必须使用以“http”开头的完整 URL 字符串。

If you're using InertiaJS, the away() approach won't work as seen on the inertiaJS github, they are discussing the best way to create a " external redirect " on inertiaJS, the solution for now is return a 409 status with X-Inertia-Location header informing the url, like this:如果您使用的是 InertiaJS,那么away()方法将无法像在inertiaJS github 上看到的那样工作,他们正在讨论在inertiaJS 上创建“外部重定向”的最佳方法,现在的解决方案是使用X-Inertia-Location返回409状态X-Inertia-Location标头通知 url,如下所示:

return response('', 409)
            ->header('X-Inertia-Location', $paymentLink);

Where paymentLink is the link you want to send the user to.其中 paymentLink 是您要将用户发送到的链接。

SOURCE: https://github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851来源: https : //github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851

return Redirect::away($url); should work to redirect应该可以重定向

Also, return Redirect::to($url);另外, return Redirect::to($url); to redirect inside the view.在视图内重定向。

For Laravel 8 you can also use对于 Laravel 8,您还可以使用

Route::redirect('/here', '/there');
//or
Route::permanentRedirect('/here', '/there');

This also works with external URLs这也适用于外部 URL

See: https://austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis请参阅: https : //austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM