How do I generate a friendly URL in Symfony PHP? - php

I always tend to forget these built-in Symfony functions for making links.

If your goal is to have user-friendly URLs throughout your application, use the following approach:
1) Create a routing rule for your module/action in the application's routing.yml file. The following example is a routing rule for an action that shows the most recent questions in an application, defaulting to page 1 (using a pager):
recent_questions:
url: questions/recent/:page
param: { module: questions, action: recent, page: 1 }
2) Once the routing rule is set, use the url_for() helper in your template to format outgoing URLs.
Recent Questions
In this example, the following URL will be constructed: http://myapp/questions/recent/1.html.
3) Incoming URLs (requests) will be analyzed by the routing system, and if a pattern match is found in the routing rule configuration, the named wildcards (ie. the :/page portion of the URL) will become request parameters.
You can also use the link_to() helper to output a URL without using the HTML <a> tag.

This advice is for symfony 1.0. It probably will work for later versions.
Within your sfAction class:
string genUrl($parameters = array(), $absolute = false)
eg.
$this->getController()->genUrl('yourmodule/youraction?key=value&key2=value', true);
In a template:
This will generate a normal link.
string link_to($name, $internal_uri, $options = array());
eg.
link_to('My link name', 'yourmodule/youraction?key=value&key2=value');

In addition, if you actually want a query string with that url, you use this:
link_to('My link name', 'yourmodule/youraction?key=value&key2=value',array('query_string'=>'page=2'));
Otherwise, it's going to try to route it as part of the url and likely break your action.

You can generate URL directly without define the rule first.
If you want to generate URL in the actions, you can use generateUrl() helper:
$this->generateUrl('default', array('module'=>'[ModuleName]','action'=>'[ActionName]'))
If you want to generate URL in the templates, you can use url_for() helper:
url_for('[ModuleName]/[ActionName]', $absolute)
set $absolute as true/false, dont forget to use echo if you want to display it.
But if you want to make a link (something like ), link_to() helper will do.

Related

Route with name "HelloWorld" not found Zend Framework 2

I have created a new module in ZF2 named 'HelloWorld'. What I am trying to do is, simply printing HelloWorld when I click on the link 'HelloWorld':
I want to generate this link(http://mayukh.my.phpcloud.com/zf2test/HelloWorld/) by using this:
$this->url('HelloWorld', array('action' => 'index'))
But it is showing the error like this:
http://mayukh.my.phpcloud.com/zf2test/
Please suggest how to avoid this error..
This is perhaps related to one of ZF2’s “features.” It seems that if you use ZF2 functions to construct your links, the function will drop out any segment that matches the default value you have named in your router script. See How to write the ZF2 router script to allow parameters on the default action.
Temporarily change or remove the defaults from your router script and see if that doesn’t solve your issue. If it does, you might have to either reconsider the scheme for your router scripts or code your links without ZF2’s url function.

Route random strings to a specific controller in CodeIgniter?

I am trying to create short links to my application in codeigniter but I've met a kind of a problem when designing my route. The problem is that I want a route which will take a string containing a-Z and numbers and redirect that to a controller called image with the string after. Like this: app.com/randomstring -> app.com/image/randomstring. But when I am trying to do this in the routes config file with a regular expression it disables my application and I am unable to enter "normal" urls with controllers that already exist.
How my route looks like right now (I know it's probably very wrongly made):
$route['(^[A-Za-z0-9]+$)'] = "image/$1";
Is there any easy way to redirect with that short url without using another fake controller first like this: app.com/i/randomstring -> app.com/image/randomstring
And could you maybe help me improve and tell me what part of my regexp is failing?
As I mentioned in the comments, without a clearly defined spec on what the image urls will be, there's no comprehensive way to solve this. Even YouTube (related to the library you linked to) uses urls like /watch?v=h8skj3, where "watch" is the trigger.
Using a i/r4nd0m$tring would make this a non-issue, and it's what I suggest, but I had another idea:
$route['(:any)'] = "image/$1";
// Re-Route all valid controllers
foreach (array('users', 'login', 'blog', 'signup') as $controller)
{
$route[$controller] = $controller;
$route[$controller.'/(:any)'] = $controller.'/$1';
}
unset($controller);
You might need the image route last, I'm not 100% sure. This should route everything to image/ except the controllers you define. You could even use glob() or something to scan your controller directory for PHP files to populate the array.
Another way to get one character shorter than i/string could be to use a character trigger, like example.com/*randomstring, but that's a little silly, i/ is much cleaner and obviously, easier to deploy.

Make title string the entire URI for all pages using CodeIgniter

With CodeIgniter I'm trying to create a URL structure that uses a title string as the entire URI; so for example: www.example.com/this-is-a-title-string
I'm pretty confident I need to use the url_title() function in the URL Helper along with the routes.php config folder but I'm stuck bringing it all together.
Where do I define the URI and how is it caught by the routes folder?
Seems to be a straight forward problem but I'm getting stuck creating the URLs end-to-end. What am I missing?
I thought about a catch-all in the routes folder: $route['(.*)'] = "welcome/controller/$1"; ....but how would this work with multiple functions inside a particular controller? ...and maybe it's not even the right way to solve.
You can send all requests to a driver with something like this:
$route['(:any)'] = "welcome/function";
Then use the _remap function to route requests inside the controller.
However, using URL's as you suggest limits the CI functionality. Try something better like www.example.com/article/this-is-a-title-string
$route['article/(:any)'] = "articles/index";
and in article (controller), use _remap...
If you're going to re-route every request, you should extend CI_Router.
The actual implementation depends on what you're doing. If you customize CI_Router, you can do it AFTER the code that checks routes.php, so that you can keep routes.php available for future customization.
If the URI contains the controller, function, and parameters, you can parse it within your extended CI_Router and then continue with the request like normal.
If the URI is arbitrary, then you'll need something (file, db, etc) that maps the URI to the correct controller/function/parameters. Using blog posts as an example, you can search for the URI (aka post-slug in WordPress) in the db and grab the corresponding record. Then forward the request to something like "articles/view/ID".

Zend framework Router replaces capital letters with dashes by default?

If we use capital alphabet in between name for zend controller and action for example inside default module we create
class MyGoodController extends Zend_Controller_Action {
public fooBarAction()
{
}
}
Than to access this action browser url looks like mysite.com/my-good/foo-bar
Is there any default zend router added inside zf managing this translation ?
because I want to use URL view helper to generate the correct link for me which it doesnt for e.g in view
$this->url(array('action'=>'fooBar','controller=>'myGood'));
did not produce the correct url it generates /myGood/fooBar instead of /my-good/foo-bar
As stated in the comment you need to use:
$this->url(array('action'=>'foo-bar','controller=>'my-good'));
The URL view helper assembles a link based on a route set in your application.
Routes match requests based on the URL.
It really comes down to separation of concerns. The helper is only making use of a route and again routes only deal with what is in the URL. Getting the proper class names based on a route is the dispatcher's concerns.
It's best to leave the route to deal with only what is in the URL because dispatchers can change. What might work for you using the standard dispatcher may not fit others that use a different dispatcher.
To accomplish what you're asking, you can always use a custom view helper that does the conversion for you but that is assuming you never change dispatchers.

How to implement keyword specific page invocation using PHP codeigniter

How to implement keyword specific page invocation using PHP codeigniter
Binary search Tree Implementation.
for e.g. in above URL keyword "binary-search-tree-implementaion" is method name or parameter for specific controller. because most of things are dynamic then how web site is going to manage all those things?
I want to implement it for my web site like this
http://example.com/search/digital-camera-price-in-india
I'm not familiar with CodeIgniter, but usually a URI structure like /search/digital-camera-price-in-india would route to the Search controller and digitalCameraPriceInIndia action.
/search/digital-camera-price-in-india
=> SearchController::digitalCameraPriceInIndiaAction()
If you want to route similar URIs (different products for example) to a catch-all method, you've have to setup custom routing.
The CodeIgniter documentation for routing is here
As per #chriso's answer you will need to set up a custom route to achieve this as by default the uri structure is /controller/action/params. So in your config/routes.php file you can add something like:
$route['search/(:any)'] =
"search/some_action";
And then use the relevant uri segment (
$this->uri->segment[1]
I think) as your search parameter.
if you set up your route like this:
$route['^search/(:any)'] = "search/my_controller_function/$1";
Then you can just write your function like this:
public function my_controller_function($search_input)
{
// your code here
}

Categories