Implementing a single module controller while leaving the controller out of the URL - url

I'd like to have one module controller implement the different action methods, but I don't want the URLs to come out like
www.example.com/module/index/action1
www.example.com/module/index/action2
www.example.com/module/index/action3
where /index/ takes us to the "IndexController". To have the URLs like I want
www.example.com/module/action1
www.example.com/module/action2
www.example.com/module/action3
I would need to create a controller class for every action method. What is the best way to get the URLs I want with the different action methods in one nice class/file/controller? I was wondering if there was a way besides URL rewrites. If not, could you point me to a good URL rewriting tutorial for Magento?

There is no real reason to violate Magento reuter/controller/action scheme. For any deviation they created the URL rewrites functional.
You can create Url rewrite even omitting the module part. Creating URL rewrites is really simple, I don't think you need tutorial for that, just open admin panel, go to Catalog->Url Rewrites Management. Click on create new rewrite, choose Custom type, add the desired uri part to Request Path (module/action), and the actual uri to the Target Path (module/controller/action).

If you don't want to do an URL rewrite at the web server, you can also do, essentially, an URL rewrite inside Magento's code. For example, you can override the function Match within Mage_Core_Controller_Varien_Router_Standard like so:
/* after getting the controller name and action name... */
if ((strcmp($module, 'myModuleName') == 0) && (strcmp($action, 'index') == 0)) {
$action = $controller;
$controller = 'index';
}
/* before checking if this place should be secure and instantiate controller class
I think URL rewrite at the web server will be cleaner and more manageable, though.
If you are using Magento's URL rewrite, you'll probably need to specify a rule for every action, such as:
Type: custom
Request Path: myModuleName/hello
Target Path: myModuleName/index/hello
You can specify these URL rewrite rules in the admin, under Catalog > URL Rewrite Management

Related

Using optional parameters in Umbraco 7 Urls

Am new to using Umbraco. I need to create Urls with an an optional parameter on the end e.g.
mysite.com/people/john
mysite.com/people/jane
etc
however by default Umbraco appears to require a separate page for each person. Is there a built method in Umbraco that will allow me to define the last part of the Url as an optional parameter or do I have to write a custom route for it?
Thanks
You have a couple of options here.
Use IIS URL Rewriting to rewrite your URLs under the hood and rewrite /people/john to /people/?person=john say. Then you can pick up the person from the query string on the page.
Write a custom URL Finder that looks for the URLs and does some stuff under the hood, like get the people page, and then set a context item with the person name in for you to use in your views etc.
You could write a custom route for it. Custom routing in Umbraco is slightly different to in normal MVC. Here is a blog post detailing how you can do it: http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/

How to use external URL as path (Rails)

I am trying to create routes within an app that I am working on like the following example:
http://www.example.com/entrepreneur.com/article/251468
My hope is to basically load an external page into an iframe by adding our domain to the URL. It needs to be without storing the external url in a database because I need every website accessable in this way. How can I do this?
You need a route with a wildcard like this:
get 'url/*args', to: 'your_controller#your_action'
See http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
I would suggest you namespace the route under some keyword to catch this wildcard route explicitly (hence url in the above).
You may need to tweak the route to allow periods to prevent them from becoming the format. I forget if that's true for these or not.

redirect root url according to locale in Zend

Working in ZF: Is it possible to change the base or root url on the www.example.com to www.example.com/$language/home, thus depending on the (browser) locale of the users browser?
Example; If a guest manually types www.example.com I would like to change the url automatically to a url with locale: www.example.com/en/home for guests in en_GB region or www.example.fr/home for guests in fr_FR region.
From the root-url I have all the menu-urls and content locale aware. Clicking a link to a menu item in the url the lang is automatically added after the root. The content on the root url is locale aware too using translate, so english for en_GB en french for fr_FR, etc.
Still missing though I would like to have the root url change to locale aware right from the start of the visit to the application if only the root is entered.
I guess something like
root :to => redirect("/prepayments")
in Rails 3 from what I understand from this Q&A on this forum
I have tried and implemented controller action helpers, redirects, etc. etc. for as far as I could find on this forum, but they all don't offer the solution.
A redirect in htaccess is not possible I think since I first need to get the locale from the browser. Since I don't know this the redirect is dynamic and I cannot set a redirect in htaccess.
I woud appreciate any suggestions?
Since I don't know what version of Zend you're using I am posting this for Zend 1.1x. One thing you could do is "catch" the request before it is dispatched, get the Accept-Language header and based on the user's preferences there set the locale or redirect the user to a URL you like. One way to do this is in the FrontController's preDispatch() method.
Keep in mind that:
You will need to check if all browsers send this through
This header string must be parsed careful
Because of the way it is defined:
Accept-Language en,bg;q=0.7,en-us;q=0.3
Also users may and often do set language preferences to a foreign language so this header on its own may not be enough.
The best way to do this in a OOP way is to wrap this functionality in a ZendPlugin.
Attach it in your bootstrap:
protected function _initLocaleHandlerControllerPlugin() {
$frontController = Zend_Controller_Front::getInstance();
$plugin = new App_Controller_Plugin_LocaleHandler();
$frontController->registerPlugin($plugin);
}
Now in this plugin redefine the preDispatch() callback:
public function preDispatch(Zend_Controller_Request_Http $request) {
$acceptLang = $request->getHeader('Accept-Language');
//Parse string and get language
//After parsing set the appropriate locale in `Zend_Locale`
//redirect the user/dispatch the request?
//THIS IS JUST AN EXAMPLE:
$request->setModuleName('home')
->setControllerName('registration')
->setDispatched(true);
}
Zend 1.x doesn't seem to have a built in class for this, Zend 2 does have the "Zend\Http\Header\AcceptLanguage" class.
I finally found the next solution on my question with a little help from Florent in Zend Framework: Redirect from action helper:
In my Language Plugin I added
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{...some code handling translate...
$url = $request->getRequestUri();
if ($url === '/') {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrl($lang.'/home');
}
The routes I have them in a routes.ini so $lang.'/home' is routed to the indexController and indexAction of the Default module.

How do I instruct Grails to always create URL:s that ends with slash (/)?

Consider the following Grails URL mapping:
class UrlMappings {
static mappings = {
"/something/${foo_id}/" {
controller = "foo"
action = "bar"
}
}
When generating URL:s using g:link ..
<g:link controller="foo" action="bar" params="[foo_id: 123]">foobar</g:link>
.. the resulting link looks like ..
foobar
Note that the ending slash in the URL mapping is removed.
However, both the URL:s /something/123 and /something/123/ still work.
Due to the requirements of the application I'm building I must make "ends with slash"-version of the URL the primary one. Ideally I'd like to make the URL not ending with slash return a 404 (to avoid canonical page issues).
What is the best and most general way to make Grails create URL:s where ending slashes are not removed as described above?
One way to solve it would be to create all URL:s manually, but I do not want to go that way.
Unfortunately grails URL mapping mechanism is not that sophisticated. So while forward URL mapping will work well (that is from URL to the controller) the reverse must be done manually to achive the described outcome.
Probably the best approach would be to create your own tag to output the desired links.
If you are looking for a way to create a groovy tag, that always add slash(/) add the end of the url, your best bet is to create a grails custom tagLib which create links, like the way g:link do.
But I don't know if that stops navigating by the link with out the slash.

Codeigniter _remap function

Please help I want to use first URI segment into my CodeIgniter website.
Like when I open these url they opens my profile:
http://www.facebook.com/buddyforever
or
http://www.myspace.com/zarpio
How can I do this with CodeIgniter? I checked _remap function but first coming controller how to hide controller?
You can do this using the URL routing of codeigniter...
If you want your URL to be http://www.mydomain.com/zarpio and you want it to refer to your_controller, then do the following.
/config/routes.php
$route['(.*)'] = "your_controller/$1"; // Now, `zarpio` will be passed to `your_controller`
You can access it in your controller like this...
$my_name = $this->uri->rsegment(2);
However I do not suggest this way of configuring URLs. A better way would be...
$route['users/(.*)'] = "your_controller/$1";
This way, you're just renaming your controller name your_controller to users.
If you want to access profile of a user, you can do it like this...
$route['users/profile/(.*)'] = "another_controller/method/$1";
$route['users/(.*)'] = "your_controller/$1";
Consider the order of routing. Since you wrote users/(.*) in your route, it will match users/zarpio as well as users/profile/zarpio, and route both of them to your_controller/$1, which in the case of profile will give you a 404 page not found error. That is why you need to write users/profile/(.*) before users/(.*) in your routing configuration.
More information in codeigniter URI class documentation

Resources