send parameters to actionIndex in Yii controllers - yii

I want to send a sql string for index action in Yii controller? something for example:
index.php?r=staff/index&id=1
I tried it and changed actionIndex() to actionIndex($id) but Yii gave me
error 400 : Your request is invalid.
Is it possible or I have to define another action ?

no you don't need to do that, receive the id as the normal request parameter inside your action method
public function actionIndex(){
$id = $_GET['id'];
}

your can get parameter as follows.
$key = Yii::app()->getRequest()->getParam('your_parameter_name');
and also you can get all the parameters from
$allParam = Yii::app()->getRequest()->restParams

OR
actionIndex($id = 0)
in your staff controller

Related

Yii2 remove form name from url parameters

I use Yii2 forms and after submit my URL looks like:
http://example.com/recommendations/index?RecommendationSearch%5Bname%5D=&RecommendationSearch%5Bstatus_id%5D=&RecommendationSearch%5Btype%5D=0&RecommendationSearch%5Bcreated_at%5D=&sort=created_at
As you may seem each parameter contains form name RecommendationSearch. How to remove this RecommendationSearch from parameters in order to get URL url like the following:
http://example.com/recommendations/?name=&status_id=&type=0&created_at=&sort=created_at
You need to override formName() in your RecommendationSearch model to return empty string:
public function formName() {
return '';
}

Phalcon router sends the wrong parameters

I have a problem with my router in Phalcon.
I have an action in my controller which ether takes a date parameter or not.
So when I access an URL: http://example.com/sl/slots/index/2017-06-27
everything works ok.
But when I go to: http://example.com/sl/slots/index
I get the following error:
DateTime::__construct(): Failed to parse time string (sl) at position
0 (s): The timezone could not be found in the database.
So the router actually takes the "sl" in the beginning as a parameter.
My router for this kind of url is set like this:
$router->add(
"/{language:[a-z]{2}}/:controller/:action",
array(
"controller" => 2,
"action" => 3
)
);
Btw it does the same withut the index: http://example.com/sl/slots
Oh and my slots index action looks like this:
public function indexAction($currentDate = false){ //code }
So the $currentDate is set to "sl" when I call the action without a parameter
Thank you for the help
Well you need to add language in first argument of action too. Then it should work.
In addition to #Juri's answer.. I prefer to keep my Actions empty or as slim as possible. Imagine if you have 3-4 parameters in the Route, you will end up with something like:
public function indexAction($param1 = false, $param2 = false, $param3 = false....)
Here is how I prefer to handle Route parameters:
public function indexAction()
{
// All parameters
print_r($this->dispatcher->getParams());
// Accessing specific Named parameters
$this->dispatcher->getParam('id');
$this->dispatcher->getParam('language');
// Accessing specific Non-named parameters
$this->dispatcher->getParam(0);
$this->dispatcher->getParam(1);
...
}

Call to a member function parameter() on a non-object in Lumen API

I got this error in my Lumen API update user module. I didn't get the Request $request values from postman. It's happening only in my UserController,
my other controllers work fine. I'm using the put method to update the user.
This is the error:
FatalErrorException in Request.php line 901: Call to a member function
parameter() on a non-object in Lumen API
My update function looks like this:
public function updateUser(Request $request,$user_id)
{
try {
$user = User::findOrFail($user_id);
} catch(ModelNotFoundException $e) {
return "User not found";
}
$user->buyer_id = $request->buyer_id;
The thing is, Lumen and Laravel use different route resolvers. You can see it for yourself if you just output the type of the variable $route just before that line 901.
Try $request['buyer_id'] instead.
I would suggest to use $request->input('buyer_id'); instead which would not throw any error if the buyer_id doesn't exist on $request stack (if it helps).
We can also pass the default value like this:
$request->input('buyer_id', null);

pass global variable between two function on zf2 controller

I should change on zf2 controller a variable global. In practice, I'd like a function to insert the value in the global variable and another function prints the value of the variable. for example:
protected $variablename;
public function setAction()
{
$this->variablename = "hi friends!";
}
public function getAction()
{
var_dump($this->variablename);
}
In this example, when I print the variable I always get NULL.
Any suggestions? tnx
simone
indirectly you can't because of the lifecircle in a zend/php request. after you set up your variable in actionA the variable is reseted if you request actionB
if you want to set your variabel data in actionA and make the data present in actionB you need a persistent save storage (like a db, cookie or session. otherwise it is only possible to forward the current request to another request in your controller. then have a look at the forward controller helper in zend.
http://framework.zend.com/manual/2.3/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-forward
//edit after comment
you can use a session in zend like this
set session
$session = new Zend\Session\Container('base');
$session->offsetSet('someSettings', 'someValue');
get session
$session = new Zend\Session\Container('base');
if( $session->offsetExists('someSettings') )
{
$someSettingValue = $session->offsetGet('someSettings');
}

MVC returns page as a file

I have an mvc4 project and I am getting a weird result. Whenever I sumit a form and fire up a postback, if the entry is correct it redirects me to the success page which is good. But when my input is invalid and it has to return to the same page, to display the error message, it comes up as a downloadable file. Can anybody please tell me what is going on?
<HttpPost()>
Public Function Collection(oColInfo As CollectionInfoVM) As ActionResult
If ModelState.IsValid Then
oColInfo.CollectionDate = DateTime.Now
m_oAppBase.Collection.AddGroupCollection(oColInfo)
Return View("_Success")
Else
ViewData.Add("PaymentTypes", PaymentType.Dictionary.ToSelectList(oColInfo.PaymentType))
ViewData.Add("PaidBy", PaidBy.Dictionary.ToSelectList(oColInfo.PaidBy.ToString()))
Return View(oColInfo)
End If
End Function
EDIT 1 :
I also found out that my controller is returning my view as JSON. That's why IE is asking me if i wanted to download it. Why is my default return type JSON?
EDIT 2 :
The response type is application/json instead of text/html.
EDIT 3 :
It works when I remove the Html.RenderAction("MainMenu", "Menu") from my layout
Controller action looks like this;
Public Function MainMenu() As PartialViewResult
' Let's see if we have an unprocessed turnin from this district
Dim dtDate As Date = DateTime.Now
Dim colDistStatus As List(Of DistrictStatus) = m_oAppBase.TurnIn.GetNextTurnInStatus()
ViewData.Add("DistrictStatus", colDistStatus)
Return PartialView("_MainMenu")
End Function
Why is my default return type JSON?
ActionResult includes JsonResult too.so when you use ActionResult and you post data from ajax ,your default returns Json.
But when my input is invalid and it has to return to the same page
for validate form in client side,you have to use valid method in your script.it validates form in clientside and doesnt post to your action.
JqueryCode:
if ($('form').valid()) {
$.ajax({});
});
Okay, so looking around I found out that there was a partial view on the page with attribute that returns JSON and when I submit the button this get's fired up too and returned to the browser. I removed the HttpPost attribute and it fixed it. sigh