dongsu1539 2016-03-09 00:35
浏览 69

尝试捕获异常并使用参数重定向

I'm working in laravel btw.

In a controller, I have some logic in a try/catch. The logic is for looking up users by ID. If a bad ID is searched, I want to catch and refresh the page with an error message like "That user ID does not exist".

I'm aware of three methods that I can use which are: back(), Redirect::to('users'), and redirect()->route('users').

Firstly, which should I use?

Secondly, I've tried each and I get error messages or not the behavior I want.

Error Messages for Each

back()

I don't see anything. I redirect to the same page just fine. But the data I pass using back()->withInput(['error' => $error]) doesn't show up on the page. I have {{isset($error) ? $error : ''}} in my template waiting for an $error variable to be set. Is there a different way to do this? Better way?

redirect()->to('users')

This acts similarly to back() in that it will redirect me just fine but still I can't seem to pass data to the page.

redirect()->route('users')

This acts similarly to back() in that it will redirect me just fine but still I can't seem to pass data to the page.

I imagine this is a simple problem. I'm just new to php and laravel and could use a little direction.

THANKS!

  • 写回答

3条回答 默认 最新

  • douwanc63652 2016-03-09 00:49
    关注
    redirect()->route('users')->with('error', "I have a error");
    
    Session::get('error')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭