I am currently under CodeIgniter 3.1.13, trying to root every single request to my Core controller. I'm starting CodeIgniter, I'm missing something as it didn't work.
I'm searching to get the bold part below as $1, and always aim the same controller.
site.com/ <- Working
site.com/test <- Working
site.com/test/test2 <- This one isn't working.
Here is my routes configuration :
$route['default_controller'] = 'Core';
$route['(:any)'] = 'Core/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = true;
I though (:any) would also handle that case.
Is there any way to do this properly ?
The Core controller is in \codeigniter\application\controllers directory.
I wonder if using REGEX couldn't work here. To be honest, I want to send the full part as $1 :
site.com/home/test/example
class Core extends CI_Controller {
// Below $page would be equal to "test/test2"
public function view($page = "index") {
}
Any idea ?
Related
$route['default_controller'] = 'Student';
$route['admin'] = 'index.php/Admin_depart/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder. but it is not working it work correctly when i write same url i the default_controller
The index.php is causing the issue.
Route destinations must be defined only as a controller/method pair. So in your case the correct route would be:
$route['admin'] = 'Admin_depart/index';
anyone browsing to yoursite.com/admin would be served whatever is in your Admin_depart controller, index method (just as if he/she browsed directly to yoursite.com/admin_depart/index;
Hey I am having an issue routing to my other controllers in codeigniter.
Basically this is my base url http://localhost:8012/CodeIgniterProject/
This works fine but every time I type something after it I get an object not found error. I am trying to route to my Dashboard.php controller but having no luck. I want to type http://localhost:8012/CodeIgniterProject/dashboard and hit the Dashboard controller.
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['home/(:any)'] = "dashboard";
$route['(:any)'] = "";
$route['404_override'] = 'dashboard';
$route['Dashboard'] = "dashboard";
$route['about'] = "site/about";
I know this might be simple to other people but I don't usually work in php and this has me a little stumped. I was under the impression codeigniter would pick up dashboard from my controllers folder and got to my controller class to load it? And then if I wanted to pass params to the class etc I'd just need to add e.g /123??? Thanks for your time.
define method too, like:
$route['Dashboard'] = "dashboard/index";
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
So I've got a mostly static site. My default controller handles most views in it's index action, simply passing the $this->uri->segment(1) through to my template.
$route['default_controller'] = 'master';
$route['(:any)'] = 'master';
$route['404_override'] = '';
But now I'm implementing a new controller that I would like to have default behavior. And I don't want to be sullying up my routes config with superfluous routes for every single action. So how can I say, route anything to the default master controller except for NewController, which you should handle normally.
Use the 404_override route location to handle your wildcard pages. That way you won't need to define every existing controller/method that you create.
Just make sure that the 404_override controller/method then correctly outputs an HTTP 404 header, and any appropriate output for the browser.
Not totally sure what you mean but if I understand you correctly if you add the line
$route['NewController'] = 'NewController';
In before the (:any) route it should load it first.
$route['default_controller'] = 'master';
$route['NewController'] = 'NewController';
$route['(:any)'] = 'master';
$route['404_override'] = '';