access controller inside the controller directory within another folder - php

This is my controller directery structure
controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.
-----index
-----Welcome.php
I can access User_reg.php like this:
http://localhost/coin_system/user/user_reg
Now I want to eliminate user from this URL so I added the route
$route['user_reg/(:any)'] = 'user/user_reg/$1';
But it show the error: 404 Page Not Found
I want to access it like this
http://localhost/coin_system/user_reg
How can i access the controller inside the directory in controller?
I have tried to solve using this SO question but it did't help.
I am using Codeigniter latest version. 3.1.5

You have missed function
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['user_reg'] = 'user/user_reg/index';
$route['user_reg/(:any)'] = 'user/user_reg/index/$1';
Or You can have different function
$route['user_reg'] = 'user/user_reg/somefunction'
$route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
Also try with index.php in url
http://localhost/coin_system/index.php/user_reg

When you add route with :any it also find your calling method after controller. But for index(which is default) its not compulsory to all index method So you need to specify route for it also. So just need to add one more route
$route['user_reg'] = 'user/user_reg'; // add this to route file
$route['user_reg/(:any)'] = 'user/user_reg/$1';

Related

is there any way to remove or hide the controller name in url? codeigniter

I developed a project in php codeigniter. The project is almost complete but now my manager wants me to completely remove or hide the controller names in the URL. My current URL looks like this:
http://www.sitename.com/Controller_Name/function_name
and my manager wants it to look like this:
http://www.sitename.com/function_name
Note that I have more than 10 controllers in my project and many many functions. I want a method that applies to all. HELP PLEASE.
You can do this using $route.
If you have only one Controller and in that controller you all the functions you can write something like this:
$route['(:any)'] = "Controller_Name/$1";
If you have many Controllers you need to specify for each function to what controller to point. Like this:
$route['function1'] = "Controller_Name1/function1";
$route['function2'] = "Controller_Name1/function2";
$route['function3'] = "Controller_Name3/function3";
And you can't have duplicates in $route
This $route array should be located here: application/config/routes.php.
You can check more about routes on the CI documentation here: https://www.codeigniter.com/userguide3/general/routing.html
You have to specify your controller and method name in routes file like below
$route['function_name'] = 'controller_name/function_name';
You have to write this for each function in controller.By doing you get two advantages
1) Controller name will be hidden in your url
2) you can call some method by its name only.No need to use controller name every time.

How to set up routing in codeigniter?

This is what I've in the config/routes.php
$route['music/artist/:any'] = "music/artist/index/$1";
And I've my controller, 'artist', inside a folder music/
I tried to get the uri segment inside the index method from the artist controller but when I go to the browser with domain.com/music/artist/abc it returns page not found.
How can I make this work? But I want to have my controller inside that folder.
Any help will be great. Thanks.
Use this in config/routes.php
$route['music/artist/(:any)/(:any)'] = "music/artist/index/$1";

codeigniter issue with loading view from controller

I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";

CodeIgniter controllers conflict with sub folder

I wrote a controller named Admin with method index and placed as /application/controllers/admin.php for URL http://localhost/admin.
Then I wrote another controller named Account inside /application/controllers/admin/ folder, with a method login for URL http://localhost/admin/account/login.
But the problem is when I visit /admin/account/login I got a 404. I don't know why? Or how can I define this 2 path in my controllers?
I also faced the same problem but for only solution was to either rename the subfolder name or rename the outside controller name.
Try put admin/account/login before admin, something like this:
$route ['admin/account/login'] = 'admin/account/login';
$route ['admin'] = 'admin/index';

How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.

Categories