I need some information about parameter passing in the phalcon framework
example URL : http://localhost/project/vehicle/new/vinno/1323123
vehicleController{
newAction {
//get the value of vinno
// want to send parameter to view
}
}
Try dumping the dispatcher parameters.
print_r($this->dispatcher->getParams());
The above will give you the complete parameters array according to your router definition. More info about dispatcher in the docs.
To get a specific parameter value you can use getParam() method.
print_r($this->dispatcher->getParam('your_param_name'));
Use route parameter. And use $this->view->setVar
Related
I have the following code in my anchor tag in my laravel application:
Submited Quality Check
My route is set to run the following method when clicked on the above link:
public function showQualityResult($qualityData) {
return $qualityData;
// return view('quality-result' , compact($qualityData));
}
Now when i click on the link, i get the following error:
Why am i getting a Missing argument 1 error when i clearly am passing data as a parameter in the tag like so below:
Submited Quality Check
Why is the parameter not being passed to the method ?
Your route from your previous question:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
To pass the parameter, you should define it. So, it'll look like this:
Route::get('showQualityResult/{data}', 'QualityCheckController#showQualityResult');
And showQualityResult() method should accept it:
public function showQualityResult($data)
{
....
I'd recommend you to read the docs to understand how it works.
I have a route for a resource controller that has a base path. In this base path there is another parameter I need: {countryId}.
Route::resource('country/{countryId}/region', 'CountryRegionController');
I know that in a regular resource controller I can get the resource's id as the function parameter, like this:
public function edit($countryRegionId)
{
}
But that is kinda messed up with the extra parameter in the route. How can I get all parameters in a conventional way?
In this case you can expand your parameter list. Actually you should expand, because if you use only one parameter that will be the value of the parameter in the base route not the id of the resource. So here is the correct way to do this.
public function edit($countryId, $countryRegionId)
{
}
Parameter name can be anything it doesn't have to match with the parameter names in the route, but the order is important.
You could also use Request::segment() but the previous solution is much better.
$countryId = Request::segment(2);
$countryRegionId = Request::segment(4);
Is there any way I can have a route set up with one regex section that doesn't get passed as a parameter?
For example:
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Specifically I'm routing to a controller and the methods need to be compatible with routes coming from elsewhere, which don't include this first parameter.
The most important thing is that, routes has to be matched according to it's declaration, for example, if you define a route like your example here
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Then the requested url has to be matched with same numbers of parameters including the http method (GET here) and in this case, the route will match only with something like this
httP//example.com/something/method/10
Here the second parameter 10 is not bound to be digits because you didn't make it to be a digit using where(...) so, it could be anything but two parameters must be required.
As an alternative, you may define a missing method in your controller like this (An idea only)
public function missingMethod($args = array())
{
// $args will contain all parameters
}
This is a special method in Laravel that any controller may contain it and whenever a non-existing method will be called in that controller, then this missingMethod would be called and all the parameters will be passed to it's $args parameter as an array, so if you have two parameters in the url and calling method is missing in the controller then you may get those parameters within this method scope, something like [param1, param2] and from this method you may call your desired method depending on the count of params.
So, if you just point the route's action to this controller which has the missing method then from the missingMethod you may call another method using different parameters according to your other method.
Also check PHP Overloading and call_user_func_array to get the real idea of missingMethod.
I am using codeigniter re-route to clean up some urls.
I am aware that I can do
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
But in some cases I have to add some extra parameters to the redirect url so that I get them as a param to the method. for example
$route['product_unique_and_rare'] = "catalog/product_lookup_by_id/{HERE I WANT SOME ADDITIONAL EXTRA PARAM}";
How to do this so that I get the value in the param of the method rather than the value in uri->resegment
you can try this
$route['product_unique_and_rare/(:num)'] = "catalog/product_lookup_by_id/$1";
Get the param in product_lookup_id like this
function product_lookup_id($product_id){
/*$product_id will be the passed parameter*/
}
So, if someone goes for http://domain.com/product_unique_and_rare/23, $product_id will get the value 23.
You can hard-code the parameter too, but I believe you aren't looking for that.
I need to capture several parameters in a controller regardless of whether the were posted or they are in the url.
Does $this->_request->getParam('parameter') work regardless?
To make life easier and shorter code, you can use the _getParam function in your controllers:
$page = $this->_getParam('page', 1);
Note that the second function variable is the default value if the request didn't include that specific variable.
Short answer, yes.
If you are in the controller, you can access any POST of GET parameter by accessing the getParam() method like you said.
$this->getRequest()->getParam("foo") will get the parameter foo, if it is present in the URL via a get param, or in a POST. It will also get any user set parameters.
The
$this->getRequest()->getParams();
Will get several parameters regardless of the action type being sent (get or post).
$this->getRequest()->getParam('foo');
Will get you individual requested parameter.
i prefer always use short function:
$parameter = $this->_getParam('parameter');