doufangmu9087 2014-07-19 13:28
浏览 53

处理laravel错误的最佳方法

I am stuck a bit, I have a little form and auth system - Sentry.

1) Get data from Input and validate it. It's okay, I get it, check with rules, set messageBag

<?php
     $validation->getMessageBag()->setFormat(Config::get('project/config.errors_delimiters'));
?>

Then I return page withErrors method. And prints validation errors.

2) Next step will be to login users, for example. This mean, that I need to use a Sentry manual.

But now I have a problem: how to show errors or store errors. I have:

  • Error messages via validation
  • Error messages via Sentry
  • Custom Errors
  • Success messages
  • Info messages

Which the best practice to store this messages? How to send it to a view? And how to parse it in a view? Also how to set delimiters style?

Of course I can create tons of flash, session, and other types of data and then send this into a view, but I think it is ridiculous. I think there is a simple method which can help me.

  • 写回答

1条回答 默认 最新

  • donglan9517 2014-07-20 15:30
    关注

    Very good question. This is how I handle it.

    Because validation errors uses MessageBag, what I did is

    $messages = new MessageBag;
    

    and then if you run into any error, you could do

    try {
        ...
    } catch (Exception $e) {
        $messages->add("error", $e->getMessage());
    }
    

    and if you have other errors returned you can merge it

    $messages->merge($other_messages)
    

    which includes validation messages $validation->messages().

    Because messagebag accepts keys, you can add other type by $messages->add('info', 'ok')

    And then in your template, or controller, you can get messages of that type by

    $messages->get('error')
    

    after you checked with

    $messages->has('error')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题