I have created a controller call posts like below
<?php
class Posts extends CI_Controller {
public function index(){
$data['title'] = 'Latest posts';
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
}
I have set routes like below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'pages/view/$1';
$route['posts'] = 'posts/index';
but if I go to the url http://localhost/Blog/posts it dies with 404 page error
But if i go to http://localhost/Blog/posts/index it works fine
why I cant get it to work in the first url? what did I do wrong please help me with this i am new to codeigniter
Looks like Blog might be a subdirectory where you have your CodeIgniter installation.
CI will treat any incoming uri as /controller/action/parameters and try to instance the corresponding Controller class. If that's the case, it's trying to instance a controller named Blog and can't find it.
To fix this you have to update your config.php and set:
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Blog/';
change $route['posts'] to $route['/posts'] in router or remove that router since index method will get called as default.
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'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.
I'm currently learning codeigniter but I'm having a bit of an issue with routing. My routes file is as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['images/(:num)/upload/'] = 'image/upload/$1';
$route['images/(:num)'] = 'image/index/$1';
$route['yoyo/(:num)'] = 'yoyo/view/$1';
$route['default_controller'] = 'yoyo';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My issue is with the first route; I'm unable to get it to work without moving (:num) to the end of the route. Based on other routes I've seen after browsing stackoverflow it get the impression that this should work. Any ideas?
So just to be clear it works if I add the follow route instead:
$route['images/upload/(:num)/'] = 'image/upload/$1';
$route['images/(:num)/upload/'] = 'image/$1/upload';
Edit: What was I thinking..sorry.
Of course you can't do the code I told you, because the route images/$1/upload means that you will access the controller images ...and the function $1.
Obviously, you can't do that. This is what you should do:
$route['images/upload/(:num)'] = 'image/upload/$1';
public class image extends CI_Controller
{
public function upload($arg0) {}
}
And then if you want to pass more args, you just need to continue the route path.
$route['images/upload/(:num)/(:any)'] = 'image/upload/$1/$2';
public function upload($arg0, $arg1) {}
I know the question has been asked many times but I really can't solve the issue.
My default controller is working, but if I want to access another controller I will have the 404 issue.
My controller file name has the upper case as Utilisateur.php,
So I'm trying to access the Controller function inscription by inscription
Here is my config:
$config['base_url'] = 'http://localhost:63342/WeBusy/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Here is my rooting:
$route['default_controller'] = 'accueil';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
And finally the controler:
class Utilisateur extends CI_Controller {
public function inscription(){
$data = array('header' => 'HeaderAccueil.php', 'body' => 'utilisateur/AjoutUtilForm.php');
$this->load->view('StructureHtml.php', $data);
}
Thanks
Give complete path to your anchor
inscription
But the best practice is to give dynamic url like using url helper or constants.
For Example if use url helper function your anchor path is like.
inscription
Don't forget to load url helper in your autoload file or your class constructor.
I found the issues,
So I needed to create a .htaccess file in the root of the applicaiton in order to remove the index.php AND this still doesn't work with phpstorm, maybe there is something to configure but it is working smoothly on EasyPhp :)
Check your controller filename, should be Utilisateur.php (ucfirst).
Check if your mod_rewrite works:
http://localhost:63342/WeBusy/index.php?q=utilisateur/inscription
add new method index() {die('test');} to your Utilisateur controller and change $route['default_controller'] = 'accueil'; to: $route['default_controller'] = 'utilisateur';
Hello i have a codeigniter project that im trying to get off the ground but im having some serious routing issues.
I followed the codeigniter official tutorial to make the news application but i rather have my news pages static then dynamically on a DB.
problem is i want to organize the pages into separate folders
views/pages = for all the sites basic pages
views/news = all the news posts
on my routes.php file i have this
$route['(:any)'] = 'news/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
i also figured id need a Controller for news so i made this
<?php
class News extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/news/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('news/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
i also included a home.php file in the news folder to see if it works but everytime i try to reach ziplinegolive.com/index.php/news/ I get a 404 error..
Does anyone have any idea how i can simply do this? I have searched ALOT for solutions but no tutorial is like mine and no one explains it simply.
The first problem I noticed is with your routing:
$route['(:any)'] = 'news/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Your first routing entry here has the same key as your second entry. So, your first entry is wiped out. And, the key (:any) will match anything, so your default_controller entry (or any other entry which comes after this one) will never be in use.
Also, I believe that in general, CI suggests that you use prettier URLs like 'news/one' instead of something like 'index.php/news/one'. If you wanted to map 'news/one' to the view method of your news controller, your routing entry could look like this:
$route['news/(:any)'] = 'news/view/$1'
And then your news controller would look something like this
class News extends CI_Controller
{
public function view( $page = 'home' )
{
$this->load->view('news/'.$page, $data);
}
}
Lastly, while I strongly recommend against using file_exists in the manner you prescribe, the defined constant APPPATH is relative, as opposed to an absolute path, which may be causing problems with your file_exists call. I would suggest using an absolute path to make sure there are no path resolution issues when checking for file existence