How to set proper route config in codeigniter framework - php

we're using codeigniter framework to build application, but we're facing problem to configure '$route' to send correct request.
we just need to setup these route properly.
How i access category:
Category: http://localhost/codeigniter/category/'category-name'/
$route['category/(:any)'] = 'category/index/$1';
Category Post List By Alphabet:
Category Page by List: http://localhost/codeigniter/category/'category-name'/list/'A'/
$route['category/(:any)/lists/(:any)/'] = 'category/lists/$1';
How i access pages:
Page: http://localhost/codeigniter/page/'category-name'/'page-name'/
$route['page/(:any)'] = 'page/index/$1/$2';
we're using rotue something like that, maybe we have problem in it, please check these and let me know how to fix that.
In our codeingiter installation we are using 'codeigniter' dir, 'category' AND 'page' are controllers. in single quotes we are sending values.

Try like
$route['page/(:any)/(:any)'] = 'page/index/$1/$2';
And
$route['category/(:any)/lists/(:any)/'] = 'category/lists/$1/$2';// But Iam not sure
$route['category/(:any)'] = 'category/index/$1';

Related

Use hyphen(-) instead of slash(/) or underscore( _ ) in Routes

I'm Using Codeigniter 3.x , Using routes.php I want to create dynamic routes, for example I have a class name Class1.
I want output url
mysite.com/Class1-Student-Search
But using hyphen(-) is not working
If I put a slash(/), it works,
$route['(:any)/Student-Search'] = "search";
it returns
mysite.com/Class1/Student-Search
and using underscore (_) also work.
$route['(:any)_Student-Search'] = "search";
returns
mysite.com/Class1_Student-Search
But I want to use hyphen(-), if I put it, it will go to 404 error page, I used these four solutions but not work for me.
$route['(:any)-Student-Search'] = "search";
$route['([a-zA-Z]+)-Student-Search'] = "search";
$route['([a-zA-Z-0-9]+)-Student-Search'] = "search";
$route['(.*)-Student-Search'] = "search";
And if i hardcode the value in route
$route['Class1-Student-Search'] = "search";
Then it also working
You trying to create a dynamic routes which is not possible in codeigniter if you see the following flow chart of codeigniter you understand what i mean.
also you can see this chart in codeigniter official website
when you try to redirect or call some url it's work like this
Every request first goes to route there for you can't make it dynamic
Here is my solution, it is working for me, do like this.
$route['(:any)-Student-Search'] = "search";
then in your link button, hopefully in your view, href the link like this.
href="/<?php echo $row->classname; ?>-Student-Search"
the point is that not only you have to make your routes, also suffix it in your href link also the same way.

How to remove different controller with same parameters from the URL in CI

I have a problem in url structure ...
I have 3 categories
streaming
movies
serials (dramas)
My Url structure is
example.com/stream/CategoryName/Channel Name
example.com/movie/CategoryName/MovieName
example.com/serial/CategoryName/Serial Name
I've added in this code in routes.php
$route['stream/(:any)/(:any)'] = "main/stream/$1/$2";
$route['movie/(:any)/(:any)'] = "main/movie/$1/$2";
$route['drama/(:any)/(:any)'] = "main/drama/$1/$2";
with this structure the website is working good. but client want that the url only contain the category name / Video Name
If i remove stream/movie/drama before /(:any)/(:any) in route then the controller get confused which function to call.
Example what i need
example.com/CategoryName/Channel Name
example.com/CategoryName/MovieName
example.com/CategoryName/Serial Name
What should i do ?
Uh this is really bad situation.
My solution would look something like this.
$route['steam-(:any)/(:any)'] = "main/stream/$1/$2";
$route['movie-(:any)/(:any)'] = "main/movie/$1/$2";
$route['drama-(:any)/(:any)'] = "main/drama/$1/$2";
But then u must edit your controller methods to explode first uri by '-' or something like that u can use parsed uri as before in method.
Hope this will help you.

Reroute with extra parameter for multi language sites in Codeigniter

In my codeigniter webapp, I'm using multi language site. Default and in english like these:
www.xxx.com (default)
www.xxx.com/en (english)
And I have a controller where I want to re-route specific calls say potato and tomato to vegie like these:
www.xxx.com/potato/param => www.xxx.com/vegie/param
www.xxx.com/tomato/param => www.xxx.com/vegie/param
So far, I have managed to reroute with default language url using like this in my route.php:
$route['potato/(.+)$'] = 'vegie/$1';
$route['tomato/(.+)$'] = 'vegie/$1';
But I doesn't work for the english site. I did like this, and not working:
$route['en/potato/(.+)$'] = 'en/vegie/$1';
$route['en/tomato/(.+)$'] = 'en/vegie/$1';
Anyone can help me for this? Thanks.
First, create new function to manage the english version, ex: function vegie_en()
then route to it
$route['en/potato/(:any)'] = 'vegie_en/$1';
I have found the problem. I have this in my route.php which makes it rerouting wrongly if there's prefix en/:
$route['en/(.+)$'] = '$1';
I moved this to the end of the route.php and it working very fine now.
I had faced same problem in my project. I fixed it by removing the 'en' and put $2 as for dynamic value.
So in your case, this will work.Please try this given below code.
$route['en/potato/(.+)$'] = 'vegie/$2';
$route['en/tomato/(.+)$'] = 'vegie/$2';

Get parameters in actions using zend framework

How to get parameters from below url
domain.com/admin/edit/12
I want to access this value (12) in edit function.
I searched around but didn't found any inbuilt solution in zend framework.
Even in other framework it works easily.
Like in codeIgnitor it works as segment and function parameter.
How I can see from code , you have Admin_Controller & some action edit ( by default routing) . For getting edit value , U need generate urls like domain.com/admin/edit/id/12/ (for example) . And than in action edit use next:
$id = $this->_request->getParam('id',0);
if ($id){
//get info for edit by ID
}
EDIT
IF you still want urls like domain.com/admin/edit/12, do next:
$uri = $this->_request->getRequestUri(); // or $this->getRequest()->getRequestUri()
$id = intval(end(explode('/',$uri)));
if($id){
// do something
}

How can I easily change the text on a page using zend framework and php

What I am looking for is something where I can easily change the text on a page. I want to set it to a default value and if something is present for a specific page to change it
So it would say something like:
I like stackoverflow
But if the value of website was "reddit" it would instead say
I like reddit
So stackoverlflow would be the default, reddit would be something that is set to overwrite it.
edit:added comment
Scenario I: Domain Name based data handling
If you want to work on the basis of domain, then in Zend Framework you can work with customize routers.
Scenario II: GET/POST based input handling
Otherwise if you want to display on the basis of GET or POST input you can place something default value as
$myDynamicVar = $this->getRequest()->getParam('some_key', 'default-value');
Scenario III: subdomain based input handling
Again you will need to have your custom router for the purpose, in addition to having support of wildcard subdomain names with your hosting provider
In a definitions file:
<?php
/* site.php
* customize this file
*/
class Site
{
static public $name = "stackoverflow";
}
?>
In your page file:
<?php
/* page.php */
include_once 'site.php';
echo "I like {Site::$name}";
?>

Categories