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.
Related
I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.
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
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.
I have controller named “profile” and the URL for user profile is
www.example.com/profile/user
I am already using the rerouting of codeigniter in the routes file
$route['profile/(:any)'] = "profile/index";
what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.
e.g
www.example.com/user
any suggestions?
You may try any one of these
// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";
Your class may look like this
class Profile extends CI_Controller {
public function index($id)
{
// $id is your param
}
}
Update : (Be careful)
Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].
You can use this in a route file:
$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
Try this:
$route['user'] = 'profile/user';
Dont know any generic way to do it
Possible duplicate of How to hide controller name in the url in CodeIgniter?
If I understand your question correctly, you just want to add a catchall to the very end of your routes file. Like:
$route['(:any)'] = 'profile/index';
This will catch everything that isn't caught by another route, so make sure this script contains logic to determine when a 404 should be presented.
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";