How to route DooPHP URIs that include querystring? - doophp

I need to be able route requests that pass information in the querystring. For example, let's say my application calls /api/company/delete?id=17. How can I route that in DooPHP? I already have a catchall route defined, which is grabbing this request, but I need to be able to process these without a catchall route.
# this doesn't work
$route['get']['/api/company/delete?id=:id'] = array('AdminController', 'deletecompany');
# at the bottom of my routes I have this catchall route, which works but it catches --everything--
$route['*']['catchall']['/:id'] = array('HomeController', 'getregions');
Am I missing something obvious?

routes
$route['get']['/api/company/delete/:id'] = array('AdminController', 'deletecompany');
controller
$this->params['id']
or
routes
$route['post']['/api/company/delete'] = array('AdminController', 'deletecompany');
controller
$_POST['id']

Related

How to use pyramid #view_defaults to specify multiple routes for same view

I'm finding I can use multiple #view_config decorators to cause a view to handle more than one route, like this:
#view_config(route_name = "user_create")
#view_config(route_name = "user_edit")
def handle_it(request):
pass
But when I try to achieve the same thing with the class decorator, #view_defaults, it doesn't work:
#view_defaults(route_name = "user_create")
#view_defaults(route_name = "user_edit")
class Foo(object):
def __init__(self, request):
self.request = request
def handle_it(self):
pass
What happens in this last case is the first route mentioned (user_create) is honored by pyramid, but request for the user_edit route produces a 404 error.
Shouldn't I be able to use #view_defaults this way?
Michael
view_defaults are used as an input to each view_config call. They are not a replacement. The only thing routing cares about is the actual views registered, which is done with view_config or config.add_view. view_defaults does not register views.

Previous page location on IronRouter

Is there a way to get the previous page location before going to the next page in IronRouter?
Is there an event I can use to fetch this information?
Thanks in advance.
Since Iron Router uses the usual History API, you can just use the plain JS method:
history.go(-1);
or
history.back();
Edit: or to check the previous path without following it:
document.referrer;
You can achieve the behavior you want by using hooks.
// onStop hook is executed whenever we LEAVE a route
Router.onStop(function(){
// register the previous route location in a session variable
Session.set("previousLocationPath",this.location.path);
});
// onBeforeAction is executed before actually going to a new route
Router.onBeforeAction(function(){
// fetch the previous route
var previousLocationPath=Session.get("previousLocationPath");
// if we're coming from the home route, redirect to contact
// this is silly, just an example
if(previousLocationPath=="/"){
this.redirect("contact");
}
// else continue to the regular route we were heading to
this.next();
});
EDIT : this is using iron:router#1.0.0-pre1
Apologies for bumping an old thread but good to keep these things up to date saimeunt's answer above is now deprecated as this.location.path no longer exists in Iron Router so should resemble something like the below:
Router.onStop(function(){
Session.set("previousLocationPath",this.originalUrl || this.url);
});
Or if you have session JSON installed (see Session JSON)
Router.onStop(function(){
Session.setJSON("previousLocationPath",{originalUrl:this.originalUrl, params:{hash:this.params.hash, query:this.params.query}});
});
Only caveats with thisis that first page will always populate url fields (this.url and this.originalUrl there seems to be no difference between them) with full url (http://...) whilst every subsequent page only logs the relative domain i.e. /home without the root url unsure if this is intended behaviour or not from IR but it is currently a helpful way of determining if this was a first page load or not

Is Angular http request call when a variable scope change?

I have to use a http get request in my angular script where I have to send some variables to the server.
My question is if the sending variable is changed somehow, then will the request call again automatically?, or do I have to call the request again??
Thanks
updated:
code in my controller:
$scope.startDate = "";
$http.get('/Controller/Action', {startDate: $scope.startDate}).success(data){
alert(data)
}
if somehow the value of the startDate is changed will the http request be called again or I have to place it into a watch.
While the question is unclear, I believe what you are referring to is a $watch setup on a scope property. If you make a normal request, such as this:
$scope.myResource = 'path/to/resource'; //could be used use without $scope for this example
$http.get($scope.myResource) //etc
the call is just made once, because that's all it is told to do. If you want it to update when the path "myResource" changes, then do this:
$scope.$watch('myResource', function(newPath) { //watching $scope.myResource for changes
$http.get(newPath) //etc
})
Now, when the value of $scope.myResource changes, the $http call will be again, this time requesting the new path.

Redirect service requests to another server

I've created a filter so that I can intercept all controller actions and forward the request to a different server.
I need to temporarily redirect based on user-agent.
So I have the following in http://www.mysite1.com
class DealsFilters {
def filters = {
all(controller: '*', action: '*') {
before = {
if (someConditionHere)
{
redirect(url:"http://www.mysite2/")
return
}
}
}
}
What I am wondering is will the request and all it params be correctly passed to mysite2?
i.e. mysite1 acts as a service and receives requests to get user data, update deals, add new users, etc...
mysite2 is a new version of mysite1 (mysite1 will be decommisioned after mysite2 has been tested).
Is it as simple as a redirect?
Thanks
I'm using grails 1.3.7 as pointed out to lucke84 (I should have stated this in original question).
Found following link gave me most of the answer:
Best practice for redirecting from one web domain to another in Grails?
redirect(url:"http://localhost:8080${request.forwardURI}?${request.queryString}",params:params)
The documentation says that if you perform a redirect with the url parameter, your url should contain all the information needed to send the new request.
I guess you should recreate your url, more or less like this:
redirect(base: 'http://www.mysite2/', controller: params.controller, action: params.action, params: params)
Not tested, but it should work for most of the cases. Let me know :)

Webforms routing with ASP.NET4

I need to provide a pageId on every URL however I don't need the user to see this pageId. For example
http://{domain}/{product-name}/{product-id}/{pageid} <-- I don't want to provide this
I have in my Global.asax:
routes.MapPageRoute("route-name", "path/{productName}/{product-id}", "~/ProductPage.aspx");
Is there some way to configure this route so it has a "hard coded" parameter page id for example something like this ---
routes.MapPageRoute("route-name", "path/{productName}/{product-id}", "~/ProductPage.aspx?pageid=1");
Is there some way to configure this route so it has a "hard coded"
parameter page id
Why yes... yes there is.
MapPageRoute has an overload that accepts a set of defaults for route values.
//Create route and set default values
routes.MapPageRoute(
"route-name",
"path/{productName}/{product-id}",
"~/ProductPage.aspx",
false,
new RouteValueDictionary{
{"product-id", 1}
});
So now if you hit this route without a "product-id" specified, it will always default to 1

Resources