Zend Framework hostname routing - zend-framework

I have the following code:
$route = new Zend_Controller_Router_Route_Hostname("test", array('controller'=>'mycontroller'));
This works fine, and redirects requests to the index action in 'mycontroller'.
What I need to do is to be able to call the following url:
http://test/myaction
Where the action called is mycontroller->myaction
Does anyone have a clue?
Thanks.

From the documentation
Hostname routes can, but never should be used as is. The reason behind that is, that a hostname route alone would match any path. So what you have to do is to chain a path route to the hostname route. This is done like in the example by calling $hostnameRoute->chain($pathRoute);. By doing this, $hostnameRoute isn't modified, but a new route (Zend_Controller_Router_Route_Chain) is returned, which can then be given to the router.
Given there are no actual variable components in your hostname route, it's quite redundant. You're better off just sticking with a Zend_Controller_Router_Route or Zend_Controller_Router_Route_Static if there are no variable parts to your routes.

Related

go-restful extract URL pattern path from request

I am using a the emicklei/go-restful framework to deal with rest API.
I wish to access the route path from the request. Meaning that when I configure a dummy route like this:
ws := new(restful.WebService)
ws.Path("/resources/names")
ws.Route(ws.GET("/{name}").To(getName))
restful.DefaultContainer.Add(ws)
I wish to access the information that the route was "/resources/names/{name}"
I can access the actual URL which is call by calling:
req.Request.URL.Path
But this will return the specific URL, not the generic one.
Any suggestion?
After more research I finally found that the method req.SelectedRoutePath() will return expected value.

Expecting arguments in Mojolicious routes

I have two routes in Mojolicious app as follows:
my $route = $r->any('/api')->to('API#');
$route->get('/get_data')->to('#process_forms');
$route->get('/get_data/?file=:file&name=:name')->to('#submit_forms');
if I go to /api/get_data I get redirected to process_forms function. I want the app to take me to submit_forms function if I pass additional arguments to that same route. for example, url /api/get_data/?file=myfile&name=myname should call submit_forms function, but that's not the case here.
In both scenarios, process_forms is called.
What option Mojolicious routing provides to help me with this?
Mojo's router connects URL and HTTP request methods to controllers. The GET and POST parameters are not used in routing. This makes sense, because a URL is typically supposed to target a resource by itself.
You have a path /get_data you need to send that to one controller. From there you want it sounds like you want to do is to go to another controller if you have GET parameters (passed in the URL). You can do this, but it's not normally what you want.
Just putting a conditional in a controller,
What you normally want when handling get parameters is simply to put them inside a block in the controller, IE,
my $query = $c->req->query_params
my $file = $query->param('foo');
my $name = $query->param('name');
if ( defined $file && defined $name) {
# we have file and name
}
else {
# or we do not
}
Redispatch
But you can always redispatch to a different controller (manually)
MyApp::Controller::submit_forms($self);
Or, through Mojo,
$self->routes->continue("#submit_forms")
As a last idea, you can also make it that the route doesn't match if the post variables aren't there. But, I've never needed to do this.

Why Zend_View_Helper_Url returns relative path when using the default route and absolute path when using a custom route?

I have an application running on Zend Framework 1.12 and I am using the URL view helper. I also have a few custom routes which I use with the URL helper to clean the code.
When I use a format such as this:
$this->url(array('module' => 'myapp', 'controller' => 'index', 'action' => 'index'), 'default', true)
The resulting string is a relative path to the action - /myapp/index/index
But when I use a custom route as such:
$this->url(array('var' => 'value'), 'custom-route', true)
The resulting string is an absolute path - http://www.domain.com/custom-route
I don't understand why is this happening and I can't find any information about this behavior online. Furthermore, I wish to know if there's a way to prevent it from happening and produce consistency in the way the URL view helper works (preferably to always return a relative path).
Thanks.
I think this is as simple as your first example being a default type Zend_Controller_Router_Route_Module route that is normally relative.
Your second example is a named route so it needs to include the base url in order to be valid. I don't believe rewritten routes can be relative as the route may or may not refer to an actual location.
I've done some limited testing and I believe that if you set your baseUrl value the first example will also include the host address. So for consistency your urls will always be absolute.
//application.ini
resources.frontController.baseurl = http://www.domain.com
Hope this helps.

Play Framework 2.1: Scala: how to get the whole base url (including protocol)?

Currently I am able to get the host from the request, which includes domain and optional port. Unfortunately, it does not include the protocol (http vs https), so I cannot create absolute urls to the site itself.
object Application extends Controller {
def index = Action { request =>
Ok(request.host + "/some/path") // Returns "localhost:9000/some/path"
}
}
Is there any way to get the protocol from the request object?
Actually there's a simple way to do it using Call class that reverse routers use to achieve similar thing.
Given that you are within the scope of implicit request, you can do something like this:
new Call(request.method, input.request).absoluteURL()
and it will provide you with the complete url (protocol, host, route and parameters).
In Play 2.3 and later you can use the secure property of the Request class.
I don't think there is.
Play Framework 2.0 itself does not support https, see: play-framework [2.0] HTTPS
The implementation of absoluteURL method of the Call class of the Play Framework 2.0 does not suggest it.
A workaround is to use a protocol relative urls using //domain.com/path.
This however does not help you with links in email. In that case you could put the protocol in the application.conf. In most cases the difference is made because production supports https and development does not.
I have yet to find a situation where the above workarounds do not work.
Actually your portnumber will give you if it's http or https.
Start your Play server with https support JAVA_OPTS=-Dhttps.port=9001 play start
Here's a code snippet (you can make the validation more stable with a regex, take the https port number from properties ...)
def path = Action { request =>
val path =
if(request.host.contains(":9000"))
"http://" + request.host + "/some/path"
else
"https://" + request.host + "/some/path"
Ok(path)
}
The code will return
http://ServerIp:9000/some/path if it's thru http
https://ServerIp:9001/some/path if it's thru https
My solution was to pass the beginning of the url as an additional parameter from javascript.
The application.conf solution does not work for me, because the same application is accessible on http and https but from different subnet and domain.

Zend framework routing params

I have several routes defined in my application.
When route A is matched and I assemble an URL using route B without resetting, it does not include the current request parameters.
Is there an easy way to include all the request parameters when assembling an URL via a different route than the current route?
I did have a look at Zend_Controller_Router_Rewrite->useRequestParametersAsGlobal, but this will (obviously) also include the request parameters when reset = true
You could try the following.
$oldParams = $this->_getAllParamas();
unset($oldParams['module']);
unset($oldParams['controller']);
unset($oldParams['action']);
Pass
array_merge(array('new'=>'param'),$oldParams)
to your URL view helper.