CakePHP 3覆盖默认路由

时间:2015-05-30 19:48:31

标签: override router cakephp-3.0

由于CakePHP 3中已删除try (InputStream is = HttpServer.class.getResourceAsStream("/config.properties") ) { properties.load(is); } ,应该使用什么来覆盖插件中Config / routes.php中设置的路由?

说我想要覆盖

Router::promote();

$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

1 个答案:

答案 0 :(得分:5)

解决方案是简单地将Plugin::routes(); Router::scope('/', function ($routes) { $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']); $routes->fallbacks('InflectedRoute'); }); 放在默认路线上...... d'哦。

package connections;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import ui.ChatWindow;

public class Chat{

private SocketChannel socketChannel;
private ConnectionType connectionType;

private ObjectOutputStream out;
private ObjectInputStream in;

private String strClientName;

private ChatWindow chatWindow;
private String strMessage;

public Chat(SocketChannel socketChannel, ConnectionType connectionType) {
    this.socketChannel = socketChannel;
    this.connectionType = connectionType;

    init();
}

private void init() {

    new Thread(this::initThread).start();
}

private void initThread() {
    try {
        out = new ObjectOutputStream(socketChannel.socket().getOutputStream());
        in = new ObjectInputStream(socketChannel.socket().getInputStream());

        if (connectionType == ConnectionType.SERVER) {
            getClientName();
            sendMyName();
        } else {
            sendMyName();
            getClientName();
        }

        chatWindow = new ChatWindow();
        chatWindow.btnSend.addActionListener(this::eventBtnSend);
        chatWindow.setTitle(strClientName);
        chatWindow.setSize(600, 400);
        chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        if (connectionType == ConnectionType.CLIENT) {
            chatWindow.setVisible(true);
            chatWindow.requestFocus();
        }

        System.out.println("here");

        beginChat();

        System.out.println("thread ends");

    } catch (IOException ex) {
        Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void getClientName() {

    try {
        strClientName = (String) in.readObject();
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void sendMyName() {

    try {
        out.writeObject(main.Config.myName);
        out.flush();
    } catch (IOException ex) {
        Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void beginChat() {
    while (true) {
        try {                
            waitForMessage();
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

private void waitForMessage() throws IOException, ClassNotFoundException {
    System.out.println("waiting for Message : " + connectionType);
    strMessage = (String) in.readObject();
    System.out.println("Message received");
    chatWindow.txtDisplay.append(strClientName + " : " + strMessage + "\n");

    if(chatWindow.isVisible() == false){
        chatWindow.setVisible(true);
    }
}

private void sendMessage(String strMessage) {

    try {
        out.writeObject(strMessage);
        out.flush();
    } catch (IOException ex) {
        Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void eventBtnSend(ActionEvent ae){
    System.out.println("a");
    sendMessage(chatWindow.txtMessage.getText());
    System.out.println("b");
    chatWindow.txtDisplay.append("Me : " + chatWindow.txtMessage.getText() + "\n");
    System.out.println("c");
    chatWindow.txtMessage.setText("");
}
}