can we give create function name like my-account in codeigniter - 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

Related

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
}

how to remove controller name in url in 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

Difficulty with two segment uri

They are trying to create the url, where the first segment is the User and the second is his file, ex: www.exemplo.com/joao/ball
Controller
public function user() {
$user_url = $this->uri->segment(1);
}
^^ This would return the profile with every file: www.exemplo.com/joao
public function arquivo() {
$arquivo_url = $this->uri->segment(2);
}
^^ This specific file: www.exemplo.com/joao/bola
Routes
$route['(:any)'] = 'home/user/$1';
$route['??'] = 'home/arquivo/$1';
To solve your problem, you should use route like this..
$route['(:any)/(:any)'] = 'home/arquivo';
$route['(:any)'] = 'home/user';
but as far you with your project this type of routing will give your some hard time. i suggest you to use explicit route name because (:any) refer any thing can be pass through this url.
You can use routes to map the URI and its parameters to the relevant function. CodeIgniter routes behave differently depending on the version of CI.
In CodeIgniter 2.2.0 (:any) is equivalent to the regex, .+ - matches one or more of any character (excluding line breaks); in 3.0 and the current development version it is equivalent to [^/]+ - one or more of any character, excluding line breaks.
The latter is more useful in this case, as you want to identify the two parameters (separated by a forward slash).
In 2.2.0:
$route['([^/]+)/([^/]+)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
In 3.0:
$route['(:any)/(:any)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
Controller functions will usually pass the URI parameters as function parameters like this:
public function user($user)
{
// Show the user's profile
}
public function arquivo($user, $file)
{
// Show the file for the user
}
Able to resolve with the following code.
$route['([^/]+)'] = 'home/user/$1';
$route['(:any)/(:any)'] = 'home/arquivo/$1';

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 - When using $route['(:any)'] = 'pages/view/$1' how to use other controllers?

When using
$route['(:any)'] = 'pages/view/$1';
and I want to use other controllers in my routing for example:
$route['del/(:any)'] = 'crud/del';
it won't work. I guess it will use
pages/view/del/$1
and not my crud-controller when deleting an item. How can I solve this?
As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:
$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
Its hundred percent working
$route['(:any)'] url is placed last in your routes file
$route['(:any)/company_product_deal_detail'] = "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)'] = "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals'] = "mypage_service_deal_list/index/$1";
$route['(:any)'] = "company/index/$1";
I know that it's an old question, but I have found myself a nice solution.
By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:
In system/core/Router.php find _parse_routes method.
Add this code under literal route match:
$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
return $this->_set_request($cont_segments);
}
I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.

Resources