remove controller and function name from url codeigniter - php

i am currently coding in codeigniter and i want to remove the controller and function name from url so that only product slug is displayed in url.
currently my url is like
www.example.com/main/singlePage/41/product-slug
I want the url to be like
www.example.com/product-slug

To do this create a "catch all" route $route['{any}'] = 'controller/function'
and in this function of the controller get the slug $slug = $this->uri->uri_string() and decide what to do based on it.

We can use routes for that, In your application/config/routes.php.
See the example,
$route['{any}'] = 'pages/slug_on_the_fly'
Ref: https://codeigniter.com/user_guide/general/routing.html

Related

Route in CodeIgniter

I am creating e-commerce website in CodeIgniter and I Have to prepare SEO friendly URL.
Currently My URL is:-
abc.com/product/product_list/1/categoryName
product is my controller
product_list is my function name
1 = category id
categoryName is my category Name
And I have to make like this:-
abc.com/categoryName
I have to remove controller name as well as function name and one uri segment i.e., category id and show only category name.
How can I do this? Is this possible using .htaccess file?
You can use route as:
$route["(:any)"] = "product/product_list/$1";
But issue is that it will hurt your other controllers like if you have page like
abc.com/contactus
It will call the product/product_list/. I suggest you to use route as:
$route["category/(:any)"] = "product/product_list/$1";
You can use URL like:
abc.com/category/yourCatName
And get category name in controller by using CI Segments.
CI Routes

how to make dynamic each function name of controller in codeigniter

I want to dynamic function name of controller in codeigniter like:
$functionName = 'profile';
function $functionName(){
------
-----
-----
}
Actually I want to change url http://website/manage/viewprfile/Username to http://website/manage/profile/Username and that url is used in whole website.
jus add this routes in application/config/routes.php
$route['manage'] = 'manage/index';
$route['manage/(:any)'] = 'manage/$1';
now u can access to manage/profile/username, manage/somethingelse
the simple way is to write route rules in following way$route['manage/prfile '] = 'manage/viewprfile';
this will change your URL to specific that you want.
Other way is to create a hook for rewriting your URL.

How to hide data from URL string on Codeigniter

I am beginner on Codeigniter so I need help.
I am making blog site with Codeigniterand I don't know how to hide controller class, and ID from URL.
Like I have generated following URL: http://localhost/codeigniter/index.php/content/some-title/123.
content is my controller class name.
some-title is my article title.
123 is my article id.
I want to keep only title to show.
For example: http://localhost/codeigniter/index.php/some-title.
Any help would be appreciated!!!
You has add your link config/routes.php
http://localhost/codeigniter/index.php/content/some-title/123
Ex: $route ['some-title/(: any)'] = 'content/some-title/$123';
You can do this:
$route['(:any)'] = 'content/view/$1';
and in your content controller you must have a method like the one follow:
public funtion view($pagetitle){…}
In the routes.php make sure you are using this line after your other routes which starts with some text.
$route['(:any)'] = 'content/show_article_post/$1';
Also make sure your slug for each article is unique in your database. There is no other way to send hidden id from get request.

CodeIgniter URI Routing for a complex URL

Problem is:
Controller: products
Method: product_details(category_name, product_name)
Current URL is: http://www.domain.com/products/product_details/category-name/product-name
But i don't want the above url, I want to enter below URL and its remapped on the above mention method
URL TO MAPPED: http://www.domain.com/product/category-name/product-name
How to do it using routes.php in codeigniter?
This should do the trick:
$route['products/(:any)/(:any)'] = 'products/product_details/$1/$2';
If your products controller has any other methods you will need to specify routes for them above this, as this is essentially a catch all route for your products controller.

Change Controller name to productname

I want to create more SEO friendly URL for my products which are stored in the database.
How can I get ProductName from the URL?
For example:
http://domain.com/ProductName
Where ProductName is not Controller. I need to validate the ProductName from the controller and display it.
Personally I would create a controller called products that accepts a parameter to find a product from the database:
class Products extends CI_Controller
{
public function index($productname)
{
$data['product'] = $this->db->get_where('products', array('urlname' => $product))->row_array();
// other functionality here and view loading
}
}
Then add routing for this scenario (config/routes.php):
$route['other/routes/first'] = "their/correct/controller/$1";
$route['/(:any)'] = "/products/index/$1";
It's been a while since I used CI but this should help point you in the right direction I hope.
You can add a routing rule in routes.php
Redirect the request to Product controller
$route['(:any)'] = "product/$1";
In your Product controller, get the product name from URI segment
$product_name = $this->uri->segment(2);
You can use the URI Routing (Routing Rules).
Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions
$route['/(:any)'] = "/products/index/$1";
this example would result in: http://www.example.com/MyProductName/ loading the products class giving the name as parameter.
You can make use of CI routes to achieve this
Edit Application/config/routes.php
$route['/(:any)'] = "/products/index/$1";

Categories