I'm new in Codeigniter I'm not sure how to use Codeigniter Routing. I've created the Contact.php in the controller folder and contact.php in the views folder.
In routes.php I have put $route['Contact'] = 'controller/contact'; but when I enter the url http://mytest.dev/contact/ it shows 404 Page Not Found. The page you requested was not found.
I want when I enter "http://mytest.dev/contact" it will show the contact page
Thanks in advance.
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('contact')
}
}
In CI there is an index.php in the URL ( by default ). So, you can access your page with this url http://mytest.dev/index.php/contact
For removing it from URL and have it like you want you need to add .htaccess file in your project directory
Check this answer for it
Also, you don't need to change your routes.php every time after creating a new page. Leave it like this
$route['default_controller'] = 'welcome'; // or contact
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I see you are using $route['Contact'] = 'controller/contact';
Tell us your controller's class name and which function/method did you use from the above you are referencing to contact() and that controller name doesn't make sense. You route will normally be in lowercase too.
If you named your class Contact (which seems like it) then you need to put a .htaccess file in the folder where your index.php or base_url resides (or root directory) and then remove the value in application/config.php as $config['index_page'] = ''; so that you can access it from http://mytest.dev/contact
To make it clearer. The format should be $route['AAA'] = 'BBB/CCC';
AAA is the url path of your choice
BBB is the name of your controller's class
CCC is the function/method of the page you want to show
If you didn't add the htaccess, you must put /index.php/ before your preferred path.
Related
Hello im having an issue here im new in learning codeigniter but my main problem is i cannot locate the url i want to have like for this:
I need to type http://localhost/ciHrs/admin/pages whereas i only want to get the url http://localhost/ciHrs/admin/ please kindly explain how can I access this url. and lastly the Default Controller I want to access http://localhost/ciHrs/admin/ directly. thanks
I have a subfolder name admin on controller and subfolder name admin on view and subfolder admin on model
here is my routes
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['admin/rooms'] = "admin/rooms/index";
$route['default_controller'] = 'admin/pages/view/dashboard';
$route['admin/(:any)'] = "admin/pages/view/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
my Controller to access this page
<?php
class Pages extends CI_Controller{
public function view(){
$data['title'] = 'Dashboard';
$this->load->view('admin/templates/header');
$this->load->view('admin/pages/dashboard',$data);
$this->load->view('admin/templates/footer');
}
}
Try the following rules on your routes :
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'admin/pages/view/dashboard';
$route['admin'] = "admin/pages";
$route['admin/'] = "admin/pages";
$route['admin/rooms'] = "admin/rooms/index";
$route['admin/(:any)'] = "admin/pages/view/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I think you are not relying to CI uri routing
EXPLAINING
On your routes:
$route['admin/rooms'] = "admin/rooms/index";
So here, you can call the domain and the corresponding admin/rooms depends on your $route definition. It looks like this: http://example.com/admin/rooms instead of http://example.com/admin/rooms/index
The default_controller seems to be loaded when the cookie session was set. You can only call the domain itself and it will load automatically the default_controller that you set.
Hence, on your question you said that you only want to get http://localhost/ciHrs/admin/, why not define in on $route just like this:
$route['ciHrs/admin/'] = "ciHrs/admin/pages";
or considering if the pages uri is dynamic set your route to:
$route['ciHrs/admin/(:any)'] = "ciHrs/admin/$1";
Hope this helps!
I have single controller and one view page when i try to load this view page using controller I am getting 404 error even route has set.
Controller:
class Navigation extends CI_Controller{
public function index(){
$this->load->view('header');
}
}
In the view directory i had place a file with the name of header and it contains as "Test Navigation" in body that's it.
Routing file:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['menu'] = 'Navigation';
This CI script placed under directory of books. So when i type as localhost/books/index.php/menu , I am getting 404 error. Please explain how to solve this.
Controller class name & controller filename should be same. class Navigation should be under Navigation.php.
Note: This rules applied for the Model too.
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";
hi my folder sturcture is like this
controllers/user/registration/register.php
Inside the register.php controller there is let say for test index function saying 'hello world'.But i cant access the folder index through browser.
My base_url is
$config['base_url'] = 'http://localhost/new/';
But while i write
localhost/new/index.php/user/registration/register/index
I got an error
The page you requested was not found.
what is weird is, I can access the controller fxn of user folder but cant access the controller fxn inside registration folder .And for default controller i have 'home.php'
$route['default_controller'] = "home";
$route['404_override'] = '';
I just want to access the controller/user/registration/register/index fxn which says 'hello world' but it says an error-'The page you requested was not found'.
Thanks
Codeigniter only supports a single level directory structure for Controllers.
Try this link below for Multi Level Subfolder Controller in CodeIgniter :
Multi Level Subfolder Controller in CodeIgniter
Ok after writing some hunch code in my test project ,finally it worked in my case
So here it goes
I follow this link Multi Level Subfolder Controller in CodeIgniter(thanks to K u s h)
http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/
and copy the code and paste in my new/application/core/MY_Router.php as told in that link
and an error came to me like this
Call to undefined method CI_Router::CI_Router() in C:\xampp\htdocs\new\application\core\MY_Router.php
So i changed a little portion of that code to
// Function MY_Router()
// {
// parent::CI_Router();
// }
public function __construct()
{
parent::__construct();
// Your own constructor code
}
And after i was able to access the controllers/user/registration/register.php index fxn
It worked in my case.Thanks to all
i was wondering whats the best way to route to pages in codeigniter? Say for example user wants to route to the index page, but should i create a method in a controller that just fo rthat page, or what is better way?
No need to create separate methods or controllers. Here's how I do it:
class Pages extends CI_Controller {
function _remap($method)
{
is_file(APPPATH.'views/pages/'.$method.'.php') OR show_404();
$this->load->view("pages/$method");
}
}
So the url http://example.com/pages/about would load the view file application/views/pages/about.php. If the file doesn't exist, it shows a 404.
You don't need any special routing to do this, but you can do something like this if you wanted the URL to be http://example.com/about instead:
// Route the "about" page
$route['about'] = "pages/$1";
// Route ALL requests to the static page handler
$route['(:any)'] = "pages/$1";
Routing can be done using the application/config/routes.php file. You can define custom routes redirecting to the index page there. There is absolutly no need to create methods for every page.
A more detailed explanation can be found here: http://codeigniter.com/user_guide/general/routing.html
EDIT:
Didn't realy got what you meant, but here is the solution I use:
class Static_pages extends CI_Controller {
public function show_page($page = 'index')
{
if ( ! file_exists('application/views/static_pages/'.$page.'.php'))
show_404();
$this->load->view('templates/header');
$this->load->view('static_pages/'.$page);
$this->load->view('templates/footer');
}
}
I make 1 controller in application/controllers for the static pages with 1 method in it that I use to load in the static pages.
Then I add this line to application/config/routes.php:
$route['(:any)'] = 'static_pages/show_page/$1';
//you can also change the default_controller to show this static page controller
$route['default_controller'] = 'static_pages/show_page';
In the config file located at /application/config/routes.php