how to remove controller name in url in codeigniter - codeigniter

My Current URL Is
http://myaliveidea.com/news/detail/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog
Then i want to remove controller name
In this URL the controller name is == detail
and function name id = hello
So i want my URL Like this
http://myaliveidea.com/news/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog

Try this code in routes.php :
$route['hello/(:any)'] = "detail/hello/$1/$2";
or
$route['hello/(:num)/(:any)'] = "detail/hello/$1/$2";

in application/config/routes.php, rewrite the url like this:
$route['news/hello/(:num)/(:any)'] = "news/detail/hello/$1/$2";
I am not sure about the news part, if it is a subfolder in the controller folder than the above line is ok, if CI in installed in news subfolder, then please remove the news part from both side.
Make sure to accept the parameter values from the hello method like this:
public function hello($id = null, $slug = null)

You can change uri path with routes, here there is documentation

Related

can we give create function name like my-account in codeigniter

I am working on codeigniter and I want to give function name like edit-profile and controller name like my-account.
I have tried to create like this but it is giving error.
In config/routes.php
$route['translate_uri_dashes'] = FALSE;
Just change to TRUE and you can use either _ or -.
and name functions separated by underscores then when request url use
separate words with dash
I hope my answer would be useful
Yes. You can
In Controller
public function my_account($value='')
{
# code...
}
In Routes
$route['my-account'] = "controller_name/my_account";
Tested. Works fine
In config
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
i hope you are asking about name conventions just through this url if about method name convention class and method naming

Code igniter url segment

currently my website url is
https://www.thefoolsbookie.com/main/inside?id=8
but I want this to be like this
https://www.thefoolsbookie.com/nfl
How can I do this?
Edit the application/config/routes.php file and add a new route for it
$route['nfl'] = 'main/inside';
It should be as simple as that :)
See https://ellislab.com/codeigniter/user-guide/general/routing.html for more examples.
Edit
I've worked with CI for a long time and I've never seen it use GET params in the routes file in that way. I'm not sure it is possible.
You could have it like $route['nfl'] = 'main/inside/8';
then in the main controller your inside method would look like:
public function inside($id)
{
//$id would contain the ID of 8 when you go to
//https://www.thefoolsbookie.com/main/inside/8
//or https://www.thefoolsbookie.com/nfl
}

Laravel 4 - Get URI of Named Route

Anyone know how to get the uri of a named route? Something like this:
$sourceUri = Route::getNamedRoute('nameOfMyRoute')->getPath(); //this doesn't work of course
The reason I want this is to determine if the current route requested by the client is the same as a given named route like this:
$targetUri = Route::getCurrentRoute()->getPath(); //this DOES work
return sourceUri === targetUri;
This one probably will work:
Route::getRoutes()->getByName('name.of.your.route')->getUri();
Use this:
$url = route('routeName', $params);

How to create username in codeIgniter?

I want address of my website user is mydomain.com/username/. But it seems very difficult to me using codeIgniter.Please help me what is best way to do it.
mysite.com/profile?user=username
is current url of profile but i want this
mysite.com/username
please help me and I'm not good english speaker so I'm sorry if you not understand my question.
In your routes.php file route every existing controller (you should have atleast one) to itself. For example if you have controller main in main.php file route it:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
The reason why you should route it twice is because you must make sure that opening http://yoursite/main works as well as http://yoursite/main/my_method/ etc. Do this for every other controller you have.
The next step is to route everything else to your users controller. For example you have a profile method that has 1 argument - the username.
$route['(:any)'] = "users/profile/$1";
So now you will have everything else routed to users/profile/username.
One thing to remember is that the topmost priority goes higher in the routes.php file so your routes file should look something like:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
$route['users'] = "main";
$route['users/(:any)' = "main/$1";
$route['(:any)'] = "users/profile/$1";
See if that works!
You can use the CI routing
In your route.php file, select all your users and create a route for each :
$aUsers = $this->oDb->getAllUsers();
foreach($aUsers as $oUser) {
$route[$oUser->username] = "profile/" . $oUser->username;
}
It should work.

Codeigniter with subfolders and uri segments

I have a site called: orders.
In the controllers folder I have a sub folder called: manage.
In there I have a controller called: editOrder
In the editOrder controller I have an index function that get an $id as a parameter.
In one of my forms I have a link to: editOrder/1
In my route file I have this code: $route['editOrder'] = 'manage/editOrder';
The link gives the error page not found.
I tried to go to the page manually, like this: http://localhost/orders/editOrder/1
Page not found
I tried this way:
In my route file:
$route['editOrder/(:num)'] = "manage/editOrder/$1";
Page not found
I have changed my config file to:
$config['uri_protocol'] = 'PATH_INFO';
$config['enable_query_strings'] = TRUE;
And tried this way:
http://localhost/orders/?c=editOrder&m=index&id=1
That takes me to the home page.
How can I pass the id segment to the editOrder controller?????
Ahhhhhhhhhh
How would I call this controller????
Make sure:
Your custom route comes after the 2 default ones. So it should be:
$route['default_controller'] = "defaultController";
$route['404_override'] = '';
$route['editOrder/(:num)'] = "manage/editOrder/index/$1";
Your controller file editOrder.php has a class editOrder extends CI_Controller and is inside the folder "controllers/manage/";
Your editOrder controller has a function index($id) {} method;
To sum up, if you're going to call a method, you need to specify it. In case of routing, that means you have to specify even the index() method.

Resources