ZF3: $request->getQuery('my_var') vs $this->params()->fromQuery('var_name')? - php

I'm learning ZF3 from this book, and I came across this:
In my controller, I can get the $_GET['var_name'] in ZF3 like this:
$request = $this->getRequest();
$request->getQuery('var_name');
Or this way:
$this->params()->fromQuery('var_name');
What's the difference between the two? (Rephrasing the question: why there are 2 ways to do the same thing?). Is one preferred over another in specific scenario?

The data is part of the request, so that's why it's held in the request object. The params controller plugin (your second example) provides a more concise way to access this data, so that's what you should use to access the data from a controller.

$this->params()->fromQuery('var_name');
here $this->params() is controller plugin, you can only use it from controller.
but for
$request = $this->getRequest();
$request->getQuery('var_name');
You can access requests from any class, as below-
$request = new Request();

Related

Adding parameters to request then get them doesn't work

I have Laravel project and in this project I have to create Request object to use it like this:
$request = new Request();
I tried to add from and to dates like this:
$request->request->add([from'=>$from_temp,'to'=>$to_temp]);
when I dd the request it gives me that from and to are exist(image)
image for request Object
but when I try to access them using
request('from')
it gives me null
I don't know what's going on
Thank you
request('from') helper will pull values form the global request object, not from the $request variable that you created. What you want to to is use $request->get('from')

Which is better: A new controller to send POST arguments or use the current one? Zend Framework 2

What type of API is this?
This is a followup question regarding an API question I had.
I am using Zend Framework 2.
zf-skeleton/module/MyApplication/src/MyApplication/Controller/IndexController.php
public function submitAction() {
$myForm = new MyForm();
$myForm->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$myModel = new MyModel();
$myForm->setInputFilter($myModel->getInputFilter());
$myForm->setData($request->getPost());
if ($myForm->isValid()) {
// Form is validated. [1]
Now the form is validated, do I send the POST arguments to another action within this controller or create a new controller?
I am looking for best practices.
As for best practices,
In a small scale application , it would be unwise to create a new controller.
But when we talk about a large scale application, each piece is done seperately, and new controller is recommended.
So, it totally depends on your application.
never use controller inside a controller.
if you want to share a common method in multiple controllers you must create a component(for cakephp framework) or a controller plugin (for zend). visit http://lab.empirio.no/custom-controller-plugin-in-zf2.html

Accessing the current request in controller

In other MVC frameworks, accessing to the current request object is as simple as $this->request. However in the Laravel, I generally see that Request $request is generally injected to each action (public function edit($id, Request $request)). It seems like a boilerplate. Is there any better way to access the request? (I now that I can use inheritance to use $this->request, I am looking for the Laravel way to do that.)
update:
I found out using app('request') I can access to the current request. However, I am not sure of its potential pros and cons.
In Laravel 5, you can use the request() helper:
// to get the current request object
$request = request();
// or to just get a value from the request
$value = request("field", "default");
See https://laravel.com/docs/5.6/helpers#method-request

Opencart: call method from another controller

I need to call in checkout/confirm.tpl file a custom function that i've made in controller/product.php
what's the best way to make this?
i've tried this, but doesn't work:
$productController = $this->load->model('product/product');
$productController->customFunction();
yes i find the right answer finally!!! sorry for last bad answer
class ControllerCommonHome extends Controller {
public function index() {
return $this->load->controller('product/ready');
}
}
MVC
in an MVC architecture, a template serves solely for rendering/displaying data; it shouldn't (*) call controller/model functions nor it shouldn't execute SQL queries as I have seen in many third-party modules (and even in answers here at SO).
$productController = $this->load->model('product/product');
nifty eye has to discover that you are trying to load a model into a variable named by controller and you are also trying to use it in such way. Well, for your purpose there would have to be a method controller() in class Loader - which is not (luckily)
How it should be done?
sure there is a way how to access or call controller functions from within templates. In MVC a callable function that is invoked by routing is called action. Using this sentence I can now say that you can invoke an action (controller function) by accessing certain URL.
So let's say your controller is CatalogProductController and the method you want to invoke is custom() - in this case accessing this URL
http://yourstore.com/index.php?route=catalog/product/custom
you will make sure that the custom() method of CatalogProductController is invoked/accessed.
You can access such URL in many ways - as a cURL request, as a link's href or via AJAX request, to name some. In a PHP scope even file_get_contents() or similar approach will work.
(*) By shouldn't I mean that it is (unfortunately) possible in OpenCart but such abuse is against MVC architecture.
$this->load->controller('sale/box',$yourData);
To call ShipmentDate() function of box Controller
$this->load->controller('sale/box/ShipmentDate',$yourData);
May be something like this could help you (or anyone who's interested)
// Load seo pro
require_once(DIR_CATALOG."/controller/common/seo_pro.php"); // load file
$seoPro = new ControllerCommonSeoPro($this->registry); // pass registry to constructor
$url = HTTP_CATALOG . $seoPro->rewrite(
$this->url('information/information&information_id=' . $result['information_id'])
);
return $this->load->controller('error/not_found');
in laravel its so simple just write Controller::call('ApplesController#getSomething');
but there i cant made better than this
$config = new Config();
// Response
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=utf-8');
$response->setCompression($config->get('config_compression'));
$this->registry->set('response', $response);
$action = new Action('product/ready');
$controller = new Front($this->registry);
$controller->addPreAction(new Action('common/maintenance'));
$controller->addPreAction(new Action('common/seo_url'));
$controller->dispatch($action, new Action('error/not_found'));
$response->output();
in this case its well call product/ready

regular querystring in Zend framework controller

I am using Zend framework and it does URL rewriting
but I want to handle in controller just regular querysting, get request
seomthing like this transactionsExternal.phppage=1&start=0&limit=100&sort=threadid&dir=ASC&callback=Ext.data.JsonP.callback1
Can somebody tell me how to get this GET request variables in controller? considering that Zend prevents me just to use variables like $_GET[something]
$this->getRequest()->getParams(); also is not returning anything
ZF does not prevent you using $_GET, although doing so is discouraged. $this->getRequest()->getParam('start'), or $this->getRequest()->getParams() will give you the GET params. So if this isn't working for you something else is going wrong somewhere.
Is 'transactionsExternal.php' definitely part of your ZF app?
How are your controllers and actions set up?
Zend Framework 1 works with key/pair values for get parameters. So you might have a transaction controller and a get action. A request would look like so:
domain.com/transactions/get
To append and ID GET parameter you would do:
domain.com/transactions/get/id/10
Then In your controller action you would do the following to get the id value:
$request = $this->getRequest();
$request->getParam('id');

Categories