Coginter Clean Url - codeigniter

I am using Coginter
i want to clean url
i have url http://androidsign.com/newarticle//categories/get_categories/electronics
i want this url http://androidsign.com/newarticle/electronics
in routes file i write code
$route['categories/(:any)'] = "$1";
But this is not working
Can anybody have idea
Thanks

Here it is,
current : http://androidsign.com/newarticle//categories/get_categories/electronics
desired : http://androidsign.com/newarticle/electronics
then on application/config/routes.php
add this routing RULE
//if class = newarticle , method = categories , 3rd seg = get_categories ,4th (:any) wildcard
$route['newarticle/categories/get_categories/:any'] = "newarticle/electronics";
A URL containing the segments newaricle/categories/get_categories will be remapped to the class newarticle and method electronics

Related

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Custem URLs inCodeigniter

I have a URL
http://www.example.com/en/category/flowers
I want this like below by removing category slug.
http://www.example.com/en/flowers
here category is flowers
The setting I have on my route config file is
$route['en/category/(:any)'] = 'category/view/$1';
And I have page urls
www.example.com/en/page/categoryname/mypage
Need to have in this format by removing "page" slug
www.example.com/en/categoryname/test-page
The setting is on route config file
$route['en/page/(:any)/(:any)'] = 'page/view/$1/$2';
What could be the solution?
You can do it like:
$route['en/flowers'] = 'page/view/1/3';
Here I am asuming that flowers category has id 1 and 3 is something else.
What about?
$route['en/(:any)/(:any)'] = 'page/view/$1/$2';
$route['en/(:any)'] = 'category/view/$1';

what is question mark means in the url in php codeigniter framework

i'm beginner in php so i want to ask this question
I read about url in Codeigniter and know url is include controller name and then method name but i didn't know what's question mark means yet ?
for example
where form post data if the url after click on submit button is
http://localhost/code/forums/topic/25674318?addpost=Publish
i'm using Codigniter may i use this url to post submitted data to model i mean insert that data to database.
This is the symbol ? which starts the begining of query in url. And after that you can use & symbol
<?php echo $this->input->get('addpost');?>
http://www.codeigniter.com/user_guide/libraries/input.html?highlight=get
Or
<?php echo $_GET['addpost'];?>
http://php.net/manual/en/reserved.variables.get.php
And then should return Publish
The fist query in url good to have ? then any other query you can use &
Example: http://www.demo.com/example/?token=123456&something=test
$var1 = 123456;
$var2 = 'test';
site_url('example/'. '?token=' . $var1 . '&somthing=' . $var2);
Config
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-&=?';

Laravel - Get route by a route name

route('products.create') returns full path like http://myapp.dev/products/create.
How do I get the actual route? Only this -> products/create?
There is no such functionality provided by Laravel at this point. However, you may try this:
$extra = URL::to('/');
$actual = route('products.create');
str_replace($extra, '', $actual);
That will remove the unnecessary base URL from your route URL.

Routing in Codeigniter (:any) gives me strange results

Here i'm using below routing config
$route['controllername/(:any)'] = "controllername/index/$1";
this is working well for me, but i want to use other methods in the same controller.
for that i'm using below route
$route['controllername/search'] = "controllername/search";
this is also working fine, but i want pass parameters to this method.
Here if i'm passing parameters but it is calling index method
i want to use both of above routes, i have tried with below route also but same result
$route['controllername/search/(:any)'] = "controllername/search/$1";
could anyone give any suggestions?
Thankyou!!
Ok.Do it like this for routing multiple functions in same controller :
$route['default_controller'] = "controller_name";
$route['index/(:any)'] = "controller_name/index/$1";
$route['search/(:any)'] = "controller_name/search/$1";
Let me know if you find any issues or doubts.
#user4039421
change your roouting rules position like below
$route['controllername/search/(:any)'] = "controllername/search/$1";
$route['controllername/search'] = "controllername/search";
$route['controllername/(:any)'] = "controllername/index/$1";

Resources