I dislike the Html->link() method. I appreciate it, but it uglies up the code and I don't believe that something so basic should require a method. However, in my project, I find I must use it if I want to have proper URLs. I'll accept that, but I want to know that I have to before continuing making links in this (large) project.
I've attempted to find some ways to get the cakeURL in a view. Nothing worked out.
My goal is to go from this:
<?=$this->Html->link('quality view', array('controller' => 'quals', 'action' => 'show')); ?>
To this:
<a href="<?=URL.'quals/show'?>">
But I cannot write or find a way to get such a constant working if I change the page's URL. (even something such as visiting /quals/ and /quals will show different URLs)
I also dislike the link method, I find it overkill for adding attributes like class, id and target="_blank" to my links.
I do this for links:
<a class="myclass" href="<?php echo $this->Html->url(array('controller'=>'my_controller','action'=>'my_action','plugin'=>false)); ?>">My Anchor Text</a>
So, I still use the HTML helper to get the URL, but the html element I code myself. I would recommend doing it that way, rather than hard-coding the URLs. This is the Cake way to do it, and it allows you to take full advantage of Cake's inbuilt routing functionality in routes.php, and create pretty routes without having to hard-code or remember them in more than one place.
It also makes it easier for others looking at your code in future - eg. does the hard-coded 'quals/show' link refer to the quals/show directory in your webroot? Or does it refer to the show action of the quals controller? (and it only gets more complex when you start working with plugins). If you use the html helper to create URL's, all that stuff is immediately clear.
If you want to make it look tidy, you could break it into two lines like this:
<?php $url = $this->Html->url(array('controller'=>'my_controller','action'=>'my_action','plugin'=>false)); ?>
<a class="myclass" href="<?php echo $url; ?>" >My Anchor Text</a>
If you've got a large project, it's even more reason to use Cake's helpers for URLs. It may seem like a good idea now, but hard coding them will give you a big headache at some point in the future.
There are actually very serious considerations for using HtmlHelper::link()!
When you're using the HtmlHelper for building links:
You have one central plase to define all routes in the application. The URL structure will always be the same as the one defined in Config/routes.php. When you change something there all links in the site will automatically reflect these changes
Reverse Routing
Easy application maintenance
SEO/Sitemapping
So if you use it you gain the abbility to change the structure of your application with minor changes, otherwise if you change one link you'd have to go through all the places it is used and change it manually.
As #joshua.paling said hardcoding the URLs is not a good idea since you'll have a lot of headaches with any change to the structure. Best is to use HtmlHelper's link() and/or url() methods.
Related
I've tried to search for the best way to handle links on a dynamic website when using "Clean" or "Pretty" URLs, but have not been able to find anything.
I've found a LOT of information on how to use mod_rewrite and try_files, which I've implemented successfully, now my php front controller parses all the parameters on a URL, and it links to the page correctly. No problem here.
The issue I'm having is how to best build all the links on my pages. Currently my links are all in the format eg. "www.site.com?do=post&id=23" which works fine. However I'd like them to display as "www.site.com/post/23" which already also works fine, because of try_files, and how my front controller parses all the parameters from the URL.
However do I now go through all my code and change all the dynamically built links to build in the "www.site.com/post/23" format? It seems like a lot of work, and to be honest I'd like to leave it an "option" to either use Clean or Dirty URLs, similar to how Wordpress allows it as an option in their Admin panel.
Do sites like that keep links in the format "www.site.com?do=post&id=23" and use a rewrite function on all the links when the page is created? So the links show up as Clean when the user sees it?
I'm confused as the best way to handle this, and hope I explained what I'm looking for. I just want to know how best to handle the dynamic links and have it optional to display as clean or dirty url format, for lack of a better word.
Thanks for any help.
That's a very conceitual question...
You could see how Laravel framework router works.
You do not need to use it, but you can get ideas there.
The router class is responsible of know how to create a url to some resource, page, action, whatever you want.
So in your view you just call a method that return the url.
So basically, what's the difference between the two approach:
<img src="<?php print base_url(); ?>img/test.png" />
<!-- and -->
<img src="/img/test.png" />
I'm having the same result on my website. Might as well remove the URL helper ($this->load->helper('url')) on my controller to cut extra process since they're just the same.
If there is an advantage of adding the helper at all, please let me know.
The difference is if you ever want to put your application in a subdirectory.
If what was / now becomes /application/ all your site root references will break.
It just makes your application free to be installed anywhere and won't require being on the site root.
In my experience with CodeIgniter, using base_url() is a best practice. It ultimately guarantees that your links will be correct (similar to the way adding ../ works when the folder you need is 1 level higher). Also I have had problems in the past with CodeIgniter adding the url in config[base_url] twice if I forgot to put http:// in it. Simple mistake, but it can throw the whole project off. So, basically the difference is that it guarantees the path is the right one, but isn't necessary at all times.
There is no difference if you're using root path of domain.
It's kind like a feature that if you're using it you can migrate easier to any domain or inside directory path.
One more advantage is when you're developing API or RSS feed, the image path or anything else will be written as complete URL so the consumer can use it easily.
I've discovered another thing usin site_url(). It is the only way I found to set a / at the end of my urls.
Eg : will result in http://domaine.tld/controller
On the other hand http://domaine.tld/controller/
hope that helps somebody
I'm developing a web application on the same domain as the "production" application -- it's just a sub directory of the top level website. Something like: www.site.com/v3/...
I've seen several people trying to get relative paths working with CodeIgniter -- in the past I had just stuck base_url() in every HTML image declaration of the "v3" site, but I bought an HTML site template recently and embedding <?php base_url() ?> in hundreds of HTML tags seems so inefficient.
Hoping someone can help with this -- I'd like to be able to transition the dev code (aka "/v3/") directly into use without searching and replacing all of those HTML references.
This is exactly the purpose of $config['base_url'] in application/config/config.php. If you set it to http://site.com/v3/, it will be reflected across your site everywhere you use base_url() or site_url()
However, I should note that using base_url() or site_url() across your entire site for every src, href, etc is bad practice. You're invoking the PHP interpreter like mad.
A much better solution is to tell your HTML document about your base using the <base> tag: <base href="<?= site_url();?>"> in the <head>.
There you have it. You use site_url() once, and from now on you can just use:
<a href='controller/action'>My Link</a> and <img src='my_image.png'/>
In general and as an overall principle, when you find yourself duplicating yourself... find a way to remove that duplication. Keep it DRY.
Thanks for all the help, folks.
After reading about the potential issues with the tag, I re-thought my approach -- I created a sub-domain of my main site (something like dev.mysite.com). That way I can work on the code exactly as it will appear on the production version, with only a base URL change in CI's config.php.
For now this seems the most simple solution, since calling the php pre-processor for every HTML tag that needs a relative path is exhausting and inefficient, and using the tag can lead to inconsistent results.
I'm redirecting all requests to index.php, which parses the URL and fires the appropriate controller based on it.
Is it a good idea to change the way query arguments appear in the URL?
like http://site.com/somepage/sub-subpage/page=20,offset=100. then parse those arguments and pass them to the controller, because it looks more readable.
Or should I stick with the $_GET thing? like ...http://site.com/somepage/sub-subpage/?page=20&offset=100
Not too good idea because you'll have to implement query parsing yourself. I see no advantages in this way. And if you use standard ?name=val&name=val notation you have:
Automatic parsing and storage to $_GET[]
Possibility to start using POST in no time.
Less possible vulnerabilites in parsing. At least they are known.
Stick with standards and therefore its better you stick with $_GET thing.
YAGNI - You ain't gonna need it. Don't think too much, just do it. Apart from a matter of taste (someone might say "I dislike questions marks in my URLs"), there is a lot of benefit to just use the common format that is just working and for which many parsers/function do exist. Additionally you find documents you can refer to if you're unclear about the format.
Its better if you got nice urls (so called SEO friend URLs) even if you dont care google or its an admin area.
The reason for using nice url is
Its more readable.
You can change the parameter by hand.
When you paste it to email, IM or other media the url makes sense.
Ugly urls makes it difficult to read the actual values. Some times you need it.
When you see the address bar it looks nice and clean and you know where you are.
In ugly urls you dont know where you are. Each url seems a middle of no-where.
Creating clean url with a little help of mod_rewrite is not tough.
Rewrite all URI to your index.php as /index.php/REQUESTED_URL
In the index.php just parse the url and invoke controller.
I think this is only interesting for SEO. Have a look at Googles opinion to this question: http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html
So it makes sense when this "pseudo" URL defines a page with really different content (i.e. /user/clara, /user/tom ...) but avoid to put a dynamic variable like session IDs into this static form.
So, I am having a big time problem.
I am using CodeIgniter. I have a website running that has a lot of pages, so URLs get too big and they look too bad. So what I want to do, is shorten my URLs. For example:
www.abc.com/main/home/promotion/deals
I want to make something like this:
www.abc.com/deals
So my question is, How should I do this thing in CodeIgniter? Is there a built in helper or library for handling my problem?
You should definitely check out the URI Routing.
In your case:
$route['deals'] = "main/home/promotion/deals";
Here is a good article if you want to actually change the URL length with a more automatic way:
http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/#removing-first-url-segment
I think there are lot of examples of the usage, so it is also like a small tutorial of how to use the routes of Codeigniter.
This is very useful especially if you are using dynamic content and the urls are automatically created (e.g. for SEO content and URLs created by your customer).