自定义Laravel FormRequest自动调整方法验证

时间:2017-09-09 15:22:39

标签: php laravel laravel-5 laravel-authorization

Laravel允许我们授权或不授权通过authorize method处理FormRequest。如果请求未获得授权,则会通过\Illuminate\Auth\Access\AuthorizationException例外,并显示消息:

  

此操作未经授权。

有什么方法可以自定义此消息吗?

看到我想自定义消息本身。 Customizing the error messages of attributes我知道这是可能的!

2 个答案:

答案 0 :(得分:4)

要更改消息,您可以将以下内容添加到conn课程中。

import socket
from threading import Thread
import Queue
import time
import logging

logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-9s) %(message)s',)

BUFFER_SIZE = 20
commands_queue = Queue.PriorityQueue(BUFFER_SIZE)

class Command(object):
    def __init__(self, priority, data, conn):
        self.priority = priority
        self.data = data
        self.conn = conn

    def __cmp__(self, other):
        return cmp(self.priority, other.priority)


class CommandsThread(Thread):
    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, verbose=None):
        Thread.__init__(self)
        self.target = target
        self.name = name


    def run(self):
        while True:
            if not commands_queue.empty():
                command = commands_queue.get()
                logging.debug("Queueing data: " + command.data)
                time.sleep(3)
                logging.debug("Finshed queue: " + command.data)
                command.conn.send("Done: " + command.data)  # echo

# Multithreaded Python server : TCP Server Socket Thread Pool
class ClientThread(Thread):
    def __init__(self, ip, port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        print "[+] New server socket thread started for " + ip + ":" + str(port)

    def run(self):
        while True:
            data = conn.recv(2048)
            print "Server received data:", data
            if not commands_queue.full():
                if data.startswith("a"):
                    commands_queue.put(Command(1, data, conn))
                else:
                    commands_queue.put(Command(2, data, conn))
                # conn.send("Done: " + data)  # echo


# Multithreaded Python server : TCP Server Socket Program Stub
TCP_IP = '0.0.0.0'
TCP_PORT = 2004


tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []

c = CommandsThread(name='commands')
c.start()
threads.append(c)

while True:
    tcpServer.listen(4)
    print "Multithreaded Python server : Waiting for connections from TCP clients..."
    (conn, (ip, port)) = tcpServer.accept()
    newthread = ClientThread(ip, port)
    newthread.start()
    threads.append(newthread)

for t in threads:
    t.join()

希望这有帮助!

答案 1 :(得分:0)

如果您尝试自定义消息authourization exception消息,则在其他部分使用授权控制器本身中的throw new exception