根据Laravel中的上一页重定向到其他页面 [英] Redirect to different pages based on previous page in Laravel

查看:599
本文介绍了根据Laravel中的上一页重定向到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,在控制器中有一个方法,例如存储一个新公司. 还要想象我们可以从站点的不同页面创建新公司.例如,我可以用2-3页创建一个公司.

Imagine you have a method in controller, like storing a new company. Also imagine we can create new company from different pages in our site. For example, I can create a company from 2-3 pages.

根据我在哪里创建公司,我需要进行不同的重定向.有时我需要重定向回去,有时需要重定向到其他路由.

Depending on where I created the company I need to have different redirects. Sometimes I need to redirect back, and sometimes to other routes.

类似:

if ($previousRoute === 'companies.index') { 
    return redirect()->back();
} else {
    return redirect()->route('someroute');
}

我想我无法获得用户来自的路线名称.如果我检查引荐来源网址,以防万一我更改路由网址,所有内容都会被破坏,因此我想依赖路由名称.另外,具有许多"if-s"或"switch"的解决方案有点奇怪,它将污染代码.

I guess I can't get the route name wher user came from. If I check referrer url then in case I change route URL everything will be broken so I want to rely on route names instead. Plus, the solution having many "if-s" or a "switch" is kind of weird, it will pollute the code.

还需要通配符支持. 带有重定向的某种路线图或类似的内容.

Also wildcard support is needed. Some kind of route map with redirects or something like this.

任何建议如何实施?

推荐答案

好吧,您可以将先前的URL与任何路由的URL进行比较.这不是最好的解决方案,我也不知道如何处理路由参数,但是它可以工作.

Well, you can compare the previous url with the url of any route. It's not the best solution and i don't know what to do with route parameters, but it can work.

if (URL::previous() === URL::route('companies.index')) { 
   return redirect()->back();
} else {
   return redirect()->route('someroute');
}

这篇关于根据Laravel中的上一页重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆