Router management in CI 3 - php

I need router management in my project.
My url is some thing like it.
http://localhost/cloud/index.php/dashboard/view_tickets/wsyZCMuIEavPeWdRHqjJ
Here i want to replace "dashboard/view_tickets" as one word "tickets"
but i have tried using routers, its not working.
Router code:
$route['ticket/(:num)'] = 'Ticket/view_tickets/$1';
Here $1 is for num, but here my parameter has string only.
Any suggestion?
Thank you.

Change this:
$route['ticket/(:num)'] = 'Ticket/view_tickets/$1';
To this:
$route['ticket/(:any)'] = 'Ticket/view_tickets/$1';
To make it work, you should create a function called "view_tickets" under "Ticket" class (Controller).
Also please see here to understand routing in Codeigniter.

Related

CI Route confliction

I am having a hard time in figuring out how to make a route for this case: http://wwww.domain.com/category-slug/product-slug i thought it was en easy task and i quickly add this line in route.php
$route['([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)'] = "products/show";
but, this is is also redirecting me on every page like http://wwww.domain.com/admin/dashboard and so on but, i explicitly want to use only for my products. How to tackle this issue?
Thats because, first segment admin and dashboard in the second matches [a-zA-Z0-9-]+ ,and remapped to the products class/controller and the show method/function.
If you can create url like something
products/category-slug/product-slug
Then if you set route like below
$route['products/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)'] = 'products/show/$1/$2';
I think you need to use something like this
$route['products/(:any)/(:any)'] = 'Product_controller/product_function/$1/$2'
Inside your function you can have something like this
public function product_function(cat_slug, pro_slug){
//Some code here
}

Custom route not working | Codeigniter Routing

I am working on an API via which I embed images of country flags on my website & several others.
I am taking in 3 parameters i.e
Country (Name of Country - ISO Code or Full Name)
Size (Dimension of Image)
Type (Styles like flat flag, shiny round flag etc...)
Now, have everything setup correctly but stuck in handling URI.
Controller -> flags.php
Function -> index()
What I have now is :
http://imageserver.com/flags?country=india&size=64&style=round
What I want
http://imageserver.com/flag/india/64/round
I went through some articles and made this route but all of them failed
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/country/$1/size/$2/style/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/$1/$2/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index?country=$1&size=$2&style=$3";
I have also been having trouble with routes while writing my custom cms. Reading through your question, I see a couple issues that might very well be the answer you are looking for.
For starters, let's look at the routes you have tried:
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/country/$1/size/$2/style/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/$1/$2/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index?country=$1&size=$2&style=$3";
If you want to run the index method from your flags class, which it looks like you do, you don't want to route to the welcome class at all. Currently, however, you are. Your routes should look like:
$route['flag/(:any)/(:num)/(:any)'] = "flags/index";
That way, Codeigniter will run the index method from your flags class. You don't have to worry about the country, size, or style/type in the route. The best option there would be to use the URI segment function like this:
$country = $this->uri->segment(2); //this would return India as per your uri example.
$size = $this->uri->segment(3); //this would return 64 as per your uri example.
$style = $this->uri->segment(4); //this would return round as per your uri example.
You could then use those variables to query your database and get the correct flag or whatever else you need to do with them.
So to restate my answer with a little more explanation as to why:
The routes you have currently are running the welcome controller/class and the index function/method of that controller/class. This, obviously, is not what you want. So you need to make sure your routes are pointing to the correct controller and function like I did above. The extra segments of the URI don't need to be in your route declaration, so you would then just use the uri_segment() function to get the value of each segment and do what you need with them.
I hope this helps you. I may not have found an answer for my problem, but at least I could provide an answer for someone else. If this seems confusing to you, check out the user guide at http://ellislab.com/codeigniter/user-guide. The main links you need for this are:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
and
http://ellislab.com/codeigniter/user-guide/general/routing.html
Let me know if you need more help or if this helped solve your problem.

CodeIgniter routing with app ID's and entry ID's

I'm new to CodeIgniter and going to be using it for building a sort of reusable application with multiple instances of an application. For example, each instance of the application will have an id "12345", and inside that instance, there will be entry IDs of 1,2,3,4,5,6,7,8, etc.
to do this, I think I will want to be able to using Routing to set up something like:
http://example.com/12345/Entry/Details/1
Where this URI will go to the Details page of the Entry of ID=1, inside application ID 12345. This would be a different group of entries from a url of, say, /12346/Entry/Details/1. Is this a routing rule that needs to be set up, and if so, can someone please provide an example of how this could be configured, and then how I would be able to use 12345, and 1, inside of the function. Thanks so much for your help, in advance.
My suggestion would be that you route your urls like this:
$route['(:any)/{controller_name}/(:any)/(:any)'] = '{controller_name}/$2/$3/$1';
so that the last parameter for the function is always the id of the app (12345/12346). Doing this means that your Entry controller functions will look like this:
class Entry extends CI_Controller
{
function Details(var1, var2, ..., varn, app_id){}
function Someother_Function (var 1, app_id){}
}
you will also need to add a route for functions that don't have anything but the app_id:
$route['(:any)/{controller_name}/(:any)'] = '{controller_name}/$2/$1'; //This may work for everything.
I hope this is what you we're asking...
Edit:
If you are only going to be using numbers you could use (:num) instead of (:any)
You can achieve a routing like that by adding this rule to the application/config/routes.php file:
$route['default_controller'] = "yourdefaultcontroller";
$route['404_ovverride'] = "";
// custom route down here:
$route['(:num)/entry/details/(:num)'] = "entry/details/$1/$2",
of course assuming your URI to be like the example.
In your controller "Entry" you'll have a method "details" which takes 2 parameters, $contestID and $photoID, where $contestID is the unique instance you're assigning, while $photoID is the other (assumed) variable of your url (last segment).
class Entry extends CI_Controller(
{
function details {$contestID, $photoID)
{ //do your codeZ here }
}
See URI routing for more info on that. You might also want to consider the __remap() overriding function, in case.

URI re-routing in codeigniter

I'm using URI re-routing in CI to make better URLS. An example here would be:
$route['users/(:any)'] = "users/index/$1";
The aim here is to get rid of the index from URL. This works well. However it stops me from being able to access any functions in the users controller, for example
mywebsite.com/users/messages
Just redirects to the users/index. Is there a way around this?
Define the list of methods you wish to keep then let the rest wildcard match:
$route['users/(messages|login|something)'] = "users/$1";
$route['users/(:any)'] = "users/index/$1";
Hi I'm not familiar with CI but i have a similar routing system. The (:any) works as a sort of catch all. When my router checks the routing rules it stops checking if it has found an exact match. So then the answer would be to just add another functions route before the catch all. Like
$route['users'] = "users/index/";
$route['users/messages/(:any)'] = "users/checkmessages/$1";
$route['users/(:any)'] = "users/$1";
Not sure how CI handles this but i can think of something like the first URL part is the class and the second the function. The router or the controller module should have the intelligence to start calling the function even without the routing table.
The routing table should only be used in case of "other callable names" like i did above with the messages/checkmessages thingy.
hope that gets you going.

Getting parameters in YII from URL

I'm total newbie on Yii. Professor basically asks us to make school project having shown us three things to do in Yii.
Let's observe two classes I have, their models being: StudProg and NivoStudija.
What I want is to pass attribute 'naziv' from nivoStudija/admin to studProg/admin, because when I click on a particular item nivoStudija/admin, studProg/admin is shown and I want to use this variable there. So I pass argument like this in one of my CGridView widget's items:
CHtml::link($data->naziv, array("studProg/admin", "nivo_naziv" => $data->naziv))
It opens up studProg/admin and I see URL like this:
http://localhost/pmf/index.php?r=studProg/admin&nivo_naziv=Osnovne+studije
My problem is: How do I get this nivo_naziv thing to use it in studProg/admin ?
Thanks in advance.
For Yii1 you need the equivalent code
$my_nivo_naziv = Yii::app()->request->getQuery('nivo_naziv);
I'm assuming that you are using Yii2.
Then you can get the URL parameter with:
Yii::$app->getRequest()->getQueryParam('nivo_naziv');
Try this for Yii1:
Yii::app()->getRequest()->getParam('nivo_naziv');
In Yii if you want to access get and post parameter, you can use getParam function like this.
Yii::app()->request->getParam('nivo_naziv);
http://www.yiiframework.com/doc/api/1.1/CHttpRequest

Categories