POST -ing data with Laravel 4's resourceful controller does not work (store method) but works using index (GET) method. What am I doing wrong? - rest

Hi I have a resource controller in Laravel 4. It has all the default methods generated by artisan's controller:make.
Models etc are in place.
User clicks on a link in a view that does a URL::route to a named route pointing at a controller action. It points to the 'store()' method in the controller, which is meant to be a POST method.
I write my code in the 'store()' method to handle this request. It uses eloquent to insert data into db. It returns a plain text response with HTTP code 200.
When user clicks on the above mentioned link (that points to the store() method), it seems the browser simply jumps to the index (GET) of that controller and the code doesn't run because the store() method is bypassed.
When I move all code from within the store() method into the index() method, everything works as expected.
What am I doing wrong here that my 'store()' method is not handling my code. Even when creating URL to the store action directly using URL::action, this fails.
Can someone please enlighten me?
Code:
Store method:
public function store()
{
$itemsArray = Session::get('sdata');
$cartItem = new Cart;
$cartItem->session_id = Session::get('sid');
$cartItem->items = json_encode($itemsArray);
$cartItem->save();
return Response::make('an item was added to carts', 200);
}
View:
Go
Same result for this view also:
`Go`

This is because <a> tag, is able to send only GET request. Try to create a new method, for example addToCart, and then set new rout on routes.php

Related

How to change action and call action/method from another controller?

I'm using Laminas framework (laminas/mvc v3.1) / Zend3. In certain cases I need to call a different action than prescribed for a given route, i.e. display a default page to users who are not authorised to view restricted content.
To do this, I've tweaked the onDispatch method in Module.php as follows:
public function onDispatch(MvcEvent $event) {
// check if content is restricted to the current user
if (content-is-restricted) {
$event->getRouteMatch()->setParam("controller", "{namespace}\Controller\{anotherController}");
$event->getRouteMatch()->setParam("action", "{defaultAction}");
$event->getRouteMatch()->setMatchedRouteName("{defaultRoute}");
}
else {
proceed-as-normal-to-the-action-prescribed-by-route
}
}
Unfortunately, the snippet above renders the following error:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
{namespace}\Controller\{anotherController}
No Exception available
This works though if the action is within the same controller but not if it's in a different one. What am I missing?
Note, I don't want to redirect the user and change the URL - just substitute what is showed to them

Struts 2 post back default

In the Struts documentation, it says:
Another common workflow stategy is to first render a page using an alternate method, like input and then have it submit back to the default execute method.
https://struts.apache.org/core-developers/action-configuration.html#post-back-default
How to do it using annotation only? It seems that only the execute() method is called.
In the documentation it's said to render a page can be used an alternate method like input. This means that when you submit a form on the page it can return back with the input result. Usually it happens automatically during validation process if the validation fails or it hasErrors. Then you can submit the form back to the default action's execute method. You don't need to specify a method in the action configuration. Also if you didn't specify the action attribute in the form tag then the same action will execute which was used to render a page.
Configuring actions you can use the same page for success result when rendering a page using GET method and input when POST method is requested.
To use annotations to configure actions mapping you can use a Convention Plugin.
Also note, to map a class method to the action you should put #Action annotation directly on this method rather than on the class.
More detailed explanation and documentation you can find here.
#Namespace("/")
public class ProductAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
#Action(value="product",
results=#Result(location="/product-list.jsp")
)
public String search() {
return SUCCESS;
}
}
Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.

Web2py return json or redirect

In web2py, I want to set up a controller that redirects for an html view but provides a json response for a json view, e.g.
http://mysite/page.html/1234 -> redirects to http://www.google.com/q=mydata
http://mysite/page.json/1234 -> returns JSON {'1234':'my data'}
(this example assumes I have a db query that returns 'mydata' for id=1234)
I can't immediately see how to do this, as the redirect() function is called in the controller, but the decision about json or html seems to be made later, once a dict() is returned from the controller
In the controller you can use request.extension to decide whether to redirect() or return response.json(my_data).
I also found that you can set {{redirect('http://site/{}'.format(data))}} in the .html view, and it will do a proper redirect within the html file itself (and set the correct content headers). In other words, the redirect function doesn't need to be called in the controller, if that's easier.

Model value not set when passing into View in ASP.NET MVC2

Stumped...
I have a few routes set up:
Function AddUpdate(ByVal id As Long) As ActionResult
Return View(New UPDATES With {.ID = id})
End Function
<HttpPost()> _
Function AddUpdate(ByVal Update As UPDATES) As ActionResult
//Do cool posted stuff here
End Function
The type of the .ID is LONG.
When it runs the post function, the Update object's Update.ID is nothing.
My view doesn't alter the property, and I don't have any Helper methods attached to it in the view. Shouldn't it be sent back through to the post method?
Thanks - JB
Is The id being sent back in the HTTP Post (using fiddler or firebug to see)?
Are you actually passing the value back with POST, either through your route, or a form field? The ModelBinder can't bind values if it doesn't know where to look for them...

Change or switch request data

I'd like to create filter which will switch request data. More precisely i'd like to change type of request inside filter (it have to be POST), add some values into post's Data, add return url, and redirect it to Controller's action which accepts only POST... and then in this action i'd like to return to first URL.
I've found something like...
Response.Redirect with POST instead of Get?
but i'm pretty sure i don't catch his idea completely and don't know is it useful in FIlter.
I've not found how to change request data... but useful was to
var controller = new MyController();
controller.ControllerContext = filterContext.Controller.ControllerContext;
controller.<action>(<parameters>); // it's action which accepts only POST, but here it doesn't matter
base.OnActionExecuting(filterContext);
Is there any better way to pass context or mayby... invoke Controller from current context? instead of creating new controller and calling his action?