Simple sub-directory pages |
Hello, As I am a beginer with CI, I am having a issue by defining sub pages which are in the sub folders.
What I want :
My Pages controller : Code: class Pages extends CI_Controller{ My Routes : Code: $route['default_controller'] = 'pages/view'; With this configuration I can access : http://localhost/mysite/contact/ But I want to access like this : http://localhost/mysite/hosting/hosting-about.php There is no internal relation between pages like parent or child, I just want to access through the sub folder within the pages folder. As I am new with CI, please help me with the broad explanation and from best practice perspectives. Thanks in Advance !
In CI, multiple segments after the controller/method path are handled as subsequent parameters.
One solution could be this: PHP Code: public function view($page = 'home', $subview=NULL){
I have done this by doing something like :
Code: public function view($page = 'home'){ And route : Code: $route['default_controller'] = 'pages/view'; But I will probably get the problem when I will have another folder inside pages/ , in that case I have set another route for that. So, yeah your way is a better solution, Can you show me how I am going to manage route in your case ? Thanks for the help. Quote:So, yeah your way is a better solution,You don't need any specific route to handle the url. Code: $route['(:any)'] = 'pages/view/$1';
It's not working when I am trying to access http://localhost/mysite/hosting/web-hosting.php
It says not found ! Quote:It's not working when I am trying to access http://localhost/mysite/hosting/web-hosting.php In CI, you don't refer to a php file, but to a controller and a method. The correct url should be: Code: http://localhost/mysite/hosting/web-hosting In this case, CI is looking for a controller called "hosting" and a method inside that controller called "web-hosting", unless you've setup routes to route the url to a different controller (and method).
I know in CI, we don't refer to directly .php file, I was just saying that I couldn't access that page.
and yeah your solution still not working. Can you check again please, thanks for the help
Try this route:
PHP Code: $route['hosting/(:any)'] = 'pages/view/hosting/$1'; In my example view function, there was a dot missing: PHP Code: if(!file_exists(APPPATH . 'views/pages/' . $view . 'php')){ To make it work, insert a . before php: PHP Code: if(!file_exists(APPPATH . 'views/pages/' . $view . '.php')){ I tested this, and on my localhost it works. Is this how you want it to work?
Your 'not found' message, is that the Codeigniter 404 message like so:
![]() Then you indeed have some routing issues. Or, if you are seeing such an error more like: ![]() Then your CodeIgniter app is not loaded at all and it's (probably) a .htaccess problem. |
Welcome Guest, Not a member yet? Register Sign In |