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!
Related
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.
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.
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'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'm using codeigniter. How can I get last part of my URL like:
www.example.com/uk-en/category/subcategory
Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"
if url is www.example.com/uk-en/category I want "category"
if url is www.example.com/uk-en I want "uk-en"
Edit
if url is www.example.com/Home/index/uk-en/category then it's working
but What i want without class name "Home" and method "index"
Like this www.example.com/uk-en/category
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index($params=[]){
$params=func_get_args();
$last=end($params);
echo $last;
}
}
?>
routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['(:any)'] = 'Home/index';
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Use this in your routs
$route['(.*)'] = "Home/index";
This to print all routs in your controller in your index function
print_r($this->uri->segment_array());
Problem in your routes is with setting wildcard placeholder on first place. It always need to be at last place.
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
//some example routes
$route['specific/route'] = 'controller/method';
$route['specific/(:num)'] = 'page/show/$1';
//always put your wildcard route at the end of routes.php file
$route['(:any)'] = 'home/index';
Docs.