I want to know whether it is possible to write pretty URLs in Code igniter or not?
I mean i have a code igniter project in which i am displaying data from uri segments like this:
http://myproj.local/category/subcategory/64/239
where 64 is the id of my category (e.g Garden Products )and 239 is my subcategory id (e.g Brushes).
Is there anyway I could write URLs like this in code igniter:
For categories:
http://myproj.local/Garden Products/
http://myproj.local/Household Range/
and for subcategories:
http://myproj.local/Garden Products/Brushes/
http://myproj.local/Household Range/Buckets/
Just like wordpress or other CMS.
IN SHORT,I WANT MY CODE IGNITER MVC framework TO DISPLAY URLs /Pages like these:
http://myproj.local/Garden Products/Brushes/
http://myproj.local/Household Range/Buckets/
Is this the kind of thing that can be achieved in the Code igniter or not??
IF YES, then how can I do this and how can i write this in code igniter ??What approach shall i use to achieve this ? I would appreciate if you could help me in this regard.Thanks
I have answered a simular question on this before.
Check this out and see if it helps.
Other options include database driven routing, take a look at this link.
Also there is an article/example on reverse routing here that may prove helpful.
SEO doesn't recommend spaces in URL
you can refer to seomoz
Instead of using IDs, you can use unique string names so http://myproj.local/category/subcategory/64/239 becomes just http://myproj.local/category/subcategory and then use routes to redirect anything that's not e.g. images, css, etc. whatever you use, to be redirected to a common controller/method which takes category and subcategory as parameters.
Also, CodeIgniter has a nice helper function url_title that will help you write unique string representations of your categories and subcategories... You just need to make sure they are unique.
The other way is like others said, make a whitelist in your routes file and make each redirection manually.
This why you have the routes file,however i know only of hardcoing categories, dunno about doing that automatically. you can do something like this :
$routes['Gardents/Products'] = "category/subcategory/64/239";
Related
I wonder what a best way to make a breadcrumb with Code Igniter.
1 : Retrieve the strings with URL
example : $this->uri->segment(2)
2 : Do you know another way ?
I'd really like to have your opinion
Using URL segments is specific to how you have your URL structured - there is not always a 1:1 match.
http://forum.codeigniter.com/thread-25372.html?highlight=137949
If you want 100% Match and easy to customize use this one :
https://github.com/nobuti/Codeigniter-breadcrumbs
Breadcrumbs is an small library that helps your manage HTML breadcrumbs with CodeIgniter.
There are some problems with buti's Codeigniter-breadcrumbs library, particularly with the "unshift" method and how it uses Codeigniter's "site_url" method instead of giving you the option to push to breadcrumbs using "base_url". You can check out my fork, since buti's repo is no longer being maintained (since 2012): https://github.com/aburd/Codeigniter-breadcrumbs
Please the following tutorial - click here to generate the breadcrumb in codeigniter.
I'm using Codeigniter(PHP Framework) and I have URLs like http://www.mysite.com/age/21/gender/female/name/jamie/city/boston/userid/1234.
Is it possible to make the URL more SEO/user friendly? Like http://www.mysite.com/1234-Jamie-Boston and somehow still be able to pass the values found in the original URL strings like age =>21 and gender => female?
I think you could - use mod_rewrite to rewrite your new url from http://www.mysite.com/1234-Jamie-Boston to http://www.mysite.com/user/1234. In your user controller load from the db the one with id 1234 and set all his other properties
Or without the mod_rewrite - you could modify your application/config/routes.php file with an entry like:
$route['(\d+)-.*'] = "users/display_user/$1";
This way your SEO friendly urls will point to class Users, method display_users and you will get the id as parameter
The way that PHP works is that if you send 15 parameters to a function which only requires two, then PHP can more or less "swallow" the other arguments (they're still accessible, but they are less than optional). In your case, I would create a Users controller, perhaps with a display function which only takes one parameter, the user ID. That way, you can have /users/display/1234/whatever/you/would/like/zodiac_sign/stop_sign/favorite_letter/favorite%20punctuation/... Need I go on?
You can get around the need for a controller/method as part of your URL by using the $routes config file... In this case, I don't think user/display would really hurt SEO. I would not use mod_rewrite to do that simply because the architecture is already there in CodeIgniter.
For example, say if a user wanted to 'add a place' to my database, how could I create a page almost instantly with that place's name in the domain e.g www.mydomain.com/placename?
I understand it might be a complex solution so if it is too complex to explain can you please push me into the right direction of what I should be researching.
Create functionality to create "pretty urls" in php. Read more about that here: http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html
Create parsing functionality for the urls, so that it recognizes "/placename" as the page "placename"
Create a database structure for pages with the page id, title, content and the url slug, etc.
Create functionality to fetch the right page from the database according to the matching url slug.
Create functionality to render the retrieved informaton
If I understood you right that's one approach to what you want to do.
I'm assuming you're using Apache. If so, create a rule using mod_rewrite that forwards requests for /place/placename to /place.php?name=placename. Then write the place.php script, which will pull the user page from the database and display it in the appropriate fashion.
That's one way to do it - there are others.
First of all try to understand mod rewrite.
You could "mask: a GET url into a much nicer format.
Start here : http://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/
Then google on and get yourself familiar with all the possibilities.
After that make sure the GET variable is unique in your database, to be absolutely sure use a unique ID.
Example :
domain.com/PLACEID/PLACENAME/
mod_rewrite could then translate this to your php script into :
domain.com/place.php?VAR=PLACEID&VAR2=PLACENAME
Search the data from the user/place through the PLACEID .
Good luck
So, here's an example on Forrst, a CodeIgniter website:
http://forrst.com/posts/PHP_Nano_Framework_WIP_Just_throwing_some_ideas_o-mU8
Look at that nice URL. You've got the root site, then posts, then the post title and a short extract. This is pretty cool for user experience.
However, my CodeIgniter site's URLs just plain suck. E.G.
http://mysite.com/code/view/120
So it accesses the controller code, then the function view, then the 20 on the end is the Post ID (and it does the database queries based on that).
I realised I could do some routing. So in my routes.php file, I put the following in:
$route['posts/(:num)'] = "code/view/$1"; - so this will make http://mysite.com/posts/120 be the same as http://mysite.com/code/view/120. A bit nicer, I think you'll agree.
My question is - how can I use a similar technique to Forrst, whereby an extract of the post is actually appended to the URL? I can't really see how this would be possible. How can the PHP script determine what it should look up in the database, especially if there are several things with the same title?
Thanks!
Jack
To get a URL like in your example you need to add a routing rule, like you've already done $route['posts/(:num)'] = "code/view/$1";. Forrst's url seems to be "mapped" (or something like that), I think the last part of the uri is the identifier (o-mU8 seems like a hash, but I prefer an int id) which is stored in the db, so if he queries, he splits the uri by the ndashes (_), and gets the last part of it, like this within your controller action:
$elements = explode('_',$this-uri-segment(2));
$identifier = $elements[count($elements)-1];
$results = $this->myModel->myQuery($identifier);
Basically the string between the controller/ and the identifier is totally useless, but not if your goal is a better SEO.
I hope this helps
See the official dicussions. The term that is often related to this is "slug". Haven't tested the approach from the CI forums myself, but the suggestions and examples look pretty good.
The URL helper in codeigniter has a function call url_title(). I haven't used it myself but I think it's what you are looking for.
I'm writing an app that allows you to filter database results based on Location and Category.
If someone was to search for Liverpool under the Golf category the URI would be /index.php/search/Liverpool/Golf.
Should someone want to search by Location but not category, they would be sent to /index.php/search/Liverpool
However, should someone want to filter only by category they would be unable to use /index.php/search/Golf because that would be caught by the location search.
Is there a best practice way to have /index.php/search/Golf be recognised? Some best practice as to what else to add to the URI to make these two queries distinct? /index.php/search/category/Golf perhaps?
Though that is beginning to show characteristics of /index.php?search&category=Golf which is exactly what I'm trying to avoid.
Try using $this->uri->uri_to_assoc(n)
described here http://codeigniter.com/user_guide/libraries/uri.html (half way down on page)
basically you will structure your url like this:
mysite.com/index.php/search/location/liverpool/category/golf
NOTE: the parameters are optional so you dont have to have both in there all the time. you can just as well do
mysite.com/index.php/search/location/liverpool/
and
mysite.com/index.php/search/category/golf
this way it will return FALSE if the element you are looking for does not exist
It would probably be best to keep your URI segments relavent no matter what they are searching for.
index.php/LOCATION/CATEGORY
If they are not interested in a location then pass a filler to the system:
index.php/anywhere/golf
Then in your code you just check for that specific string of ANYWHERE to determine if they only want to see the activity. I assume that you are going to be redirecting them with either links or forums (and that they aren't typing the URI string themselves) so you should be safe in just passing information that you expect and testing against that.
I use the format suggested by Tom above and then do something along the lines of below to determine the value of the parameters.
$segment_array = $this->uri->segment_array();
$is_location_searched = array_search('location', $segment_array);
if($is_location_searched && $this->uri->segment($is_location_searched +1))
{
$location = $this->uri->segment($is_sorted+1);
}
Have a look at http://lucenebook.com/#/p:solr/s:wiki and click around a bit on the left-hand navigation. Pay close attention to what happens in the url when you do. I really like this scheme for many reasons.
It's SEO-friendly.
"Curious" people can mix/match the urls and it still resolves to a proper search.
It just looks good!
Of course, the trick is really in the code, in how you build the thing. It took me a few weeks to sort it out, but I finally have my own version of that site. Just not ajax based, because I like search engines better than ajax. Ajax don't pay the bills.