We have an online-shop.
We can have any friendly urls
as for separate models like products or categories (/iphone-6-white = /iphones/23)
as for custom filter urls like (/cool-flashes = /flash?cap=32gb&col=white)
What is the best practice to handle all urls?
IMHO we should create a table where we should store two cols (urlFrom and RedirectUrl). But redirect is not good way for us because we do not want a redirect, we just want to show appropriate content under the urlFrom.
I want to store it in a single table to make just one request to DB to find out whether we have the url or not.
You can create submit your form in array such as search[name], search[price][form] and create your custom Middleware that will checks request contain of search[] build url and after that redirect to this page.
Of course after that you must create route for those page, like:
Route::get('/search/{searchName?}/{searchPriceFrom?}', 'ProductsController#search');
Where your RoutesServiceProvoider will look like this:
Route::bind('searchName', function($term) {
// your DB query and checks...
});
Related
In My Application
We can access user profile screen by any of next URLs format
http://example.com/users/123/
http://example.com/users/123/mike
http://example.com/users/123/mike-pedro
But the problem is that
Now many URLs will show the same content
Because i dont care about last {slug}, i only use {id} to show the content
And according to
http://blog.codinghorror.com/url-rewriting-to-prevent-duplicate-urls/
That will lowers my PageRank
And divvied up between the 3 different URLs instead of being concentrated into one of them.
When i checked stackoverflow implementation
I found
The 3 different URLs will directed to the same content and the same URL
for example
All next 3 links
https://stackoverflow.com/users/1824361/
https://stackoverflow.com/users/1824361/yajli
https://stackoverflow.com/users/1824361/yajli-maclo
ALL Will directed to one target URL and show its content
https://stackoverflow.com/users/1824361/yajli-maclo
The target link = {id} + {slug}
How to implement that using codeigniter
In your controller you can change the method where you'll be sending the ID to a model (with 1 argument - $id) that will return the "slug" (name) from the table for that specific ID.
With these you can then call another method in the controller that will take 2 arguments (Id and slug). This will make the link look like this: example.com/users/123ID/blaSlug
So if you access the first method, he will do the job and go to the second method.
Hope this helps
Cheers
I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.
I'm making a site using Codeigniter and my URL for a particular product page is like http://www.domain.com/products/display/$category_id/$product_id/$offset
$offset is used for limiting the number of pages shown per page when using the Codeigniter's Pagination library.
How I want to make it such that my URL is something more human friendly, like http://www.domain.com/$category_name/$product_name/$offset ie. http://www.domain.com/weapons/proton-canon/3
Can anyone point me the general approach? I just started learning codeigniter and is working on my first project
You can use what's generally known as a URL slug to achieve this.
Add a new field to your table, called "url_slug" or similar. Now you will need to create a slug for each product, and store it in this field.
CI has a function in the URL helper - url_title($string) which will take a string, and convert it for use in URL's.
For example My product name would become my_product_name.
Now, in your method, you can either - keep the product_id intact, use this as a parameter for your method to show specific products, and use the slug for human friendly links, or you can just use the url_slug to refer to products.
Your URL may look like:
www.domain.com/$category_name/$product_id/my_cool_product/$offset
or it could look like
www.domain.com/$category_name/my_cool_product/$offset
with no ID. the choice is yours, but the url_slug may change - the ID won't. Which may have SEO impacts.
Regardless, your method needs to look something like:
function display_product($product_id, $url_slug, $offset) {
// do what you gotta do...
}
You can then use URL's like the above.
You will need to use URI routing as well, as the example above will attempt to look for a controller called $category_name and a method called my_cool_product, which will of course not exist.
See URI Routing for further info.
I know that CodeIgniter already elegantly handles URL's. What I have is a form with multiple elements (date, keyword, location = optional). Is it possible to set up CI to create a URL that looks like:
mysite.com/class/function/date/keyword?
If you are using CodeIgniter's form helper, it sends your form data via POST, so you can't easily have your field's value displayed in the url.
What you could have, though, is a controller method that collects your form data and redirects to the url you want.
If you need further clarification, please let me know.
Yes, you can use the anchor function by passing the relevant info to it.
I am trying to code my first codeigniter project. I have a login controller which basically filters the data inputed and calls a model function that checks if the user is found in the database.
What I am trying to do is reuse this controller on the index page. So basically I want to be able to do user login on the index page or on the normal controller page (index.php/login/) without code duplication.
I'm sure there is an easy way to do this, but I'm not sure what the best solution is. Make it a library?
Thanks!
For this I would simply make the form in your view post to the login controller.
As a more generic way to share code and logic throughout your application, take a look at this article:
CodeIgniter Base Classes: Keeping it DRY
You basically give each of your controllers a "type". Being logged in could be a criteria of one of your base controllers, which saves you trying to directly access any of your controllers which is bad mojo.
You can try creating a form on the index page and submit it to index.php/login/. This way you won't need two entry points.
Just do the same as you have done for the login View, specify the same action attribute of the form to the index View, and it will be sent to the same login controller with no need to create the two login controllers. You might want to append a query string in the action attribute of the form to distinguish from which View the request has come.