What $this->_forward is doing

 

Question:

Hello everyone,

I would like someone to explain me what _forward is exactly doing, I cannot see if _forward is also rendering the attached view to the action or just executing the action.

Also is it possible to pass argument to $this->action in a view script ?

More generally my problem is how to code a confirmation page, let's say the user input some stuff and you want to show him confirmation, is forward is mean for that case ?

 

 

Answer1:

_forward is an internal redirect. Where as _redirect sends a header that tells the client's browser to go to some other URL, _forward tells the Dispatcher to internally redirect the request somewhere else.

If you consider the normal dispatch order of:

 preDispatch() 
 someAction() 
 postDispatch() 

Calling _forward at any point in that progression will cause the following steps to not be executed. So if you call _forward in preDispatch(), someAction() will not be called and so on. If you _forward() in someAction() and you are using the viewRenderer action helper to render your views (you are letting the framework choose what view script to render), then no view script will be rendered in someAction().

When the request is forwarded to the new Controller / Module the entire dispatch process will be repeated there.

You can find out what action is being dispatched by using:

 $action = $this->getRequest()->getParam('action'); 

$action will be the url form of the action so if the method is name 'someKindOfAction', $action will contain 'some-kind-of'. You can do this as well for controllers and modules.

 

Answer2:

I think it's important to note that _forward is very inefficient, and you should always call your method directly. When you do a _forward, the init(), pre and post dispatch run again. Depending on what you have in your init, you can run (and insert) the same database record twice.

It is easy to use but wasteful. If you profile your code, and are banging your head to why everything is being called twice, _forward is the reason. If your like me and you instantiate a few objects in the init() for use throughout the class, you wind up instantiating everything twice! I did load testing on my code and I got better performance by calling the action name directly, like foo(), instead of _forward('foo');

Another off topic tip I think most people know, is it use single quotes wherever possible, sine the PHP parser has to check a string for embedded variables. I don't know how much real world performance this will give, especially if you are using an opcode cache, but it's a best practice.

 

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtWidgets> #include <QResizeEvent> //绘图区域大小变化事件 #include <QDebug> #include <QThread> #include <QTimer> #include <QVector> #include <QRandomGenerator> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->plot->setOpenGl(true); ui->plot->setNoAntialiasingOnDrag(true); CreateChart(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::CreateChart() { ui->plot->addGraph(); ui->plot->graph(0)->setPen(QPen(QColor(100,149,237))); mData = ui->plot->graph(0)->data()->coreData(); ui->plot->xAxis2->setVisible(true); ui->plot->xAxis2->setTickLabels(false); ui->plot->yAxis2->setVisible(true); ui->plot->yAxis2->setTickLabels(false); connect(ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->plot->xAxis2, SLOT(setRange(QCPRange))); connect(ui->plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->plot->yAxis2, SLOT(setRange(QCPRange))); ui->plot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectPlottables); ui->plot->yAxis->setRange(-1.5,1.5); ui->plot->xAxis->setRange(0,50); ui->plot->yAxis->setLabel("数据"); ui->plot->xAxis->setLabel("个数"); for (int i = 0; i < 50; i++) { mXDataTol.append(i); mYDataTol.append(QRandomGenerator::global()->bounded(1.00)); } ui->plot->graph(0)->setData(mXDataTol,mYDataTol,true); ui->plot->replot(); }这是我的全部代码,系统报错:terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc请帮我修改一下代码,我不知道到底哪里出了错
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

paypal1987

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值