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) {}
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 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.
I have a CI Project where I want to change the URL like below:
www.example.com/index.php/Controller/method/param1
to
param1.example.com/index.php/Controller/method
Is this possible using just CI? Like routes. Or we need to .htaccess?
If possible, are there any risks or is it a bad practice?
Also does it affect SEO?
EDIT: This rewrite should happen only for Controller controller.
EDIT: I think the meaning came out wrong. When a user enters a URL like
param1.example.com/Controller/method It has to fire the method in Controller and pass param1 as parameter.
You need to create redirect controller (controllers/Redirect.php), that will redirect browser Something like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Redirect extends CI_Controller {
public function index($method,$domain) {
$redirect_url = 'http://www.' . $domain . '.example.com/index.php/Controller/' . $method;
redirect($redirect_url,'refresh');
}
}
?>
Add this line in application/config/route.php
$route['Controller/(:any)/(:any)'] = 'redirect/index/$1/$2';
You are done. Enjoy :)
Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...
In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".
Inside my home.php controller I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct() {
// blah
}
function index(){
// blah
}
function login(){
// blah
}
}
Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?
If I just have to create a new one, is there a best practices for how many controllers you have, etc.
Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
So use $route['login'] = 'home/login';
The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.
You need add a line to your application/config/routes.php file
$route['login'] = "home/login";