regarding URLs / Titles |
Hello,
I inherited the task of maintaining a page for a small, niche-market classic car dealership that was done in CI with HTML, CSS layout and a mySQL DB...Not entirely over my head, but I'd be lying if I said I had a lot of previous experience with MVC. Anyow, the site is fairly basic, and is mostly composed of details about each car, and slider photo galleries along with basic inquiry forms. The dev wrote an Admin panel of sorts that lets you quickly add and subtract vehicles, but it offers very little outside of that in terms of doing any quick customizations and the like. So, with that, one of the issues I am facing with the site in it's current form is page titles....Currently, each vehicle profile page has a page title like "www.blahblahblah.com/index.php/inventory/detail/69" This appears to be bad for SEO purposes, so my question is, what would it take to get CI to use the vehicle's title (i.e. 1959 Roll-Royce Phantom) to be used for the URL - like ""www.blahblahblah.com/index.php/inventory/detail/1959-rolls-royce-phantom" If someone could point me in the direction of help files or guides related to these types of topics I would greatly appreciate it - very very eager to learn, thanks! I have also been tasked with creating a new website or the same company, and would like to use CI, as there seems to be less overhead than a platform like Wordpress. Thanks again for any help here!
The docs: http://www.codeigniter.com/docs
Some high points, assuming you're using CI3: - Overview: http://www.codeigniter.com/user_guide/ov...index.html - Tutorial: http://www.codeigniter.com/user_guide/tu...index.html - CodeIgniter URLs: http://www.codeigniter.com/user_guide/general/urls.html - URI Routing: http://www.codeigniter.com/user_guide/ge...uting.html - Controllers: http://www.codeigniter.com/user_guide/ge...llers.html More than likely, index.php/inventory/detail/69 is routing to a controller named Inventory and passing 69 to a method named detail(). The best way to change this to handle a URL like "www.blahblahblah.com/index.php/inventory/detail/1959-rolls-royce-phantom" would be to start with the detail() method. More than likely the number is going to be a key value, and the detail() method will load a model and call a method on the model to look up the details of the car based on that key. Take a look at the model and determine whether it already has a method to look up the details based on the vehicle's title in the form you will be able to use in the URL. If it does, this is going to be an easy process. I would most likely attempt to retrieve the vehicle by title given the input. If that fails and the input is numeric, attempt to look it up by key. If the key lookup succeeds, I would grab the title from the returned data and redirect to the titular form of the URL, or use it to supply a canonical URL for SEO. If the model doesn't have a method to look up the vehicle by title, or the titles in the database aren't suitable for use in a URL, you will probably need to modify the model to provide that functionality. You may also decide to modify the database to add a column to store URL-friendly titles to make the lookup process easier. (10-28-2015, 12:57 PM)mwhitney Wrote: The docs: http://www.codeigniter.com/docs wow, thank you so much for your help with this - I have unfortunately got called on to perform other duties today (I essentially handle all IT/Tech duties for the company), but I am going to take a look at this tonight from home. In the meantime, here is the "details" controller / page code for each vehicle: Code: <?php $this->load->view('_inc/_head'); ?> and here is the "inventory" controller/page code: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Many many thanks once again and I'll see what I can accomplish later this evening. The other thing that has been a debacle to get going is a recaptcha on the details page inquiry form, but that 's likely my just incompetence with CI
Back to work here, yesterday I posted the incorrect code for the Inventory controller. From my limited experience, I am not seeing anything in the CI controller or the DB with respect to titles - here is the Inventory controller:
Code: class Inventory extends CI_Controller { Here's a few screengrabs from the current DB, which has the vehicles by stock number, year, make, model: The head include does include a title tag, but outside of that I see nothing with respect to URLs. Thanks for any feedback here, the documentation is helpful, but rather daunting at this stage!
OK, first, it looks like your detail method in the Inventory controller calls your Inventory_model's get_record() method, but the controller doesn't grab the number from the URL, so I'm guessing the model is doing that (in a perfect world the model wouldn't do that, but it looks like your model may be doing that in get_records_or_make() and get_archived(), as well).
Since you don't have a field in the database that's already populated with the information you want to use in the URL, you have to decide whether you want to specify a way to map the URL to fields in the database, or create a new column to use to lookup the correct entry in the database. For example, if you go to "/inventory/detail/1959-rolls-royce-phantom", you could do something like this: PHP Code: $lookup = $this->uri->segment(3); The problem should be readily apparent, as you have the year (1959), but the make, 'rolls-royce' (I'm assuming), is split across two entries in the array, while other makes would be a single entry, and still others might be more, and the model would have the same issue. One common approach to solving this problem is to create a "slug" (what I previously referred to as a title) from the fields and insert that as a new column in the database. So, your new column would have a value of '1959-rolls-royce-phantom' for that particular row in the database (you could write a script to populate this column after creating it in the database, using the production_year, make, and model to build the "slug"; the url_title() function in CI's URL helper tends to be very useful for something like that). Another alternative is to change the URL format that you would like to use. For example, you could use "/inventory/detail/1959/rolls-royce/phantom". Then you could do something like this: PHP Code: $year = $this->uri->segment(3); or even: PHP Code: public function detail($year, $make, $model) It looks like your model's get_makes() method might already do something to deal with looking up the make from the URL, but the make() method of the controller uses urldecode($this->uri->segment(3)), which tells me the model's method may not be SEO-friendly. So, you might still need a column (or two) to hold SEO-friendly versions of the make (and probably model) names, or a lookup table (or two) to map the SEO-friendly names to the names used in the existing make and model columns. |
Welcome Guest, Not a member yet? Register Sign In |