codeigniter routes any or num not working - php

I have url like that http://lp.dev/sisters/adab/1 but the route is not working when i use (:num) or (:any) to get the value 1 because the route give me 404 page
routes as follows
$route['default_controller'] = "frontend/home";
$route["sisters/adab/(:num)"] = "frontend/pages/$1"; //<-- this is my issue
$route['404_override'] = 'errors/error_404';
controller : pages.php inside frontend folder
class Pages extends CI_Controller {
function __construct() {
parent::__construct();
$this->name = $this->uri->segment(2);
}
public function index($variable = NULL)
{
dd($variable);
if(is_page($this->name))
load_view("$this->name/home");
else
load_view('errors/error_404');
}
}

I guess you want this
$route["sisters/adab/(:num)"] = "frontend/pages/index/$1"; //correct
$route["sisters/adab/(:num)"] = "frontend/pages/$1"; // is wrong because
//it is redirecting to your page's controller and looking for a method (:num)

Related

how to passing parameter to index and custom route

I want to pass parameters to index function and if the parameter is not, redirect to home page. i added custom route also. where did i miss the code?
$route['main/(:any)'] = 'main/index/'; // looks like has a mistake
www.example.com ->when deafult index function
www.example.com/females ->when deafult index has string parameter 'female'
class Main extends CI_Controller {
public function index($gender) {
load->view('inc/header_view');
if((isset($gender)) && ($gender =='female')){
$this->load->view('female_view');
} else {
$this->load->view('female_view');
}
}
You can achieve this by following code
class Main extends CI_Controller {
public function index($gender="") {
if($gender == "")
{
$this->load->view('home');
} else {
$this->load->view('female_view');
}
}
Also as you mentioned to get this URL to work www.example.com & www.example.com/females , you need to add following into your routes.php
$route['default_controller'] = "main";
$route['(:any)'] = 'main/index/$1';
Let me know in case of any queries
You should try it like
$route['main'] = 'main/index';
$route['main/(:any)'] = 'main/index/$1';
Or you can use uri segments.
http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri%20segments#CI_URI::segment

unable to load second controller

In codeIgniter I have created routes to remove the controller/methodname from the url so but the problem is that when I try to access the second controller index method it is not loading it's loading the home controller
I have 2 controller Home.php and Admin.php when i type in localhost/foldername/ it is opeing home conrtollers index method but when I type in localhost/foldername/admin it is redirecting to home controller's second method which is page can any one help me out in writing the routes.
Here is my routes which I have created
$route['default_controller'] = 'home';
$route['([^/]+)/?'] = 'home/page/$1'; //If I comment this it is working properly for me
Admin Controller Admin.php
class admin extends CI_Controller {
public function index() {
if($this->session->userdata('is_logged_in') == true) {
$this->load->view('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
}
Home Controller Home.php
class home extends CI_Controller {
public function index() {
$front_page = $this->get_data->front_page();
$page_data = $this->get_data->AllData('pages', $front_page);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
$page_content = $page_data->row()->template;
$this->load->view($page_content, $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
public function page($id) {
$page_data = $this->get_data->AllData('pages', $id);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
if($class[1] == 'home') {
$data['slider'] = 'templates/slider';
}
$data['content'] = $page_data->row()->template;
$this->load->view('index', $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
}
Now can anyone help me out to solve this issue one thing more when I comment the custom routes it is working perfectly but the home controller for the page which i tried to remove method and controller name it is coming as 404 not found
According to what i undertand, Set your default controller, to check your authentication first:
$route['default_controller'] = 'admin';
//so that you can check weather the user is logged in or not.
To access your second controller's index function:
$route['Home'] = "home";
To access your second controller's page($id) function:
$route['Home/Page/(:num)'] = "home/page/$1";
// where num id the ID you will be passing to the page function.

CodeIgniter route is not working, throwing 404 error

I am new to CodeIgniter. I am trying to load a route, but it's not working. Here is my code:
ROUTE:
$route['default_controller'] = 'Welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['register'] = 'Register_controller'; // Not working
$route['register/thankyou'] = 'Register_controller'; // Not working
Register Controller:
class Register_controller extends CI_Controller {
public function index()
{
$this->load->view('register');
}
public function thankyou()
{
$this->load->view('thankyou');
}
}
VIEW :
I HAVE register.php and thankyou.php page on view section
Why is it not working?
It's displaying a CodeIgniter 404 page.
I think you should add method name in controller as following.
$route['register'] = 'Register_controller/index';
$route['register/thankyou'] = 'Register_controller/thankyou';
or
$route['register'] = 'register_controller/index';
$route['register/thankyou'] = 'register_controller/thankyou';

Redirect to alias in Codeigniter

Hi I need to redirect to alias in url with codeigniter... for example
If the user type in the browser http://www.website.com/fedex, need to get the fedex like alias and find in the database by this alias and redirect to the correct url that must be http://www.website.com/pages/fedex... here is my code...
PD: By the way do not exists controller named fedex.
.. routes.php ...
$route['default_controller'] = "alias";
$route['404_override'] = '';
$route[':any'] = "alias/index/$1";
.. pages Controler ..
class Alias extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$alias = $this->uri->segment(1);
echo "Alias:" . $alias;
}
}
I think this is what you are trying to do...
class Alias extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
}
public function index(){
echo 'Default page!';
}
public function get($alias)
{
$this->db->from('users')->where(array('username' => $alias));
$query = $this->db->get();
if ($query->num_rows() > 0){
//This means that a row was returned from the database and their is a user that exists with that name
redirect("/pages/$alias");
}
else{
echo 'There is no user that exists with that username';
}
}
public function pages($alias){
echo "Hey $alias!";
}
}
//Routes
$route['default_controller'] = "alias";
$route['404_override'] = '';
$route['pages/:any'] = "alias/pages/$1";
$route[':any'] = "alias/get/$1";
In the constructor it loads the database class and the URL helper. In the index() method is uses the database to select a row from the "users" table where their username is the $alias. Then it checks with an if statement if a row was returned from the database. If a row was returned that means a user exits and to redirect to /pages/username. Else it will echo saying no one exits with that username.
Make sure to change the database info match your database setup as well as make sure $active_record is set to TRUE in the database config file.

username as controller codeigniter

I am trying to develop a social network where people can visit mysite.com/username. I used the _remap function and I got it working, however it is not loading any of my other controllers. Can someone be of assistance please?
This is my default controller:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index($username = NULL)
{
$this->load->model('user_model');
if ($this->user_model->is_a_username($username)) {
$data['title'] = $username;
$data['main_content'] = 'users/profile_page';
$this->load->view('shared/template',$data);
} else {
$this->home();
}
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
public function home()
{
if ($this->ion_auth->logged_in()) {
$data['title'] = 'Carnect';
$data['main_content'] = 'users/wall_page';
$this->load->view('shared/template',$data); #if logged in show the user's wall
} else {
$data['title'] = 'Carnect';
$data['main_content'] = 'welcome/index';
$this->load->view('shared/template',$data); #if not logged in show the home page
}
}
}
and this is my routes file:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/create_user";
/*$route['news'] = "news/index";
$route['politics'] = "politics/index";
$route['culture'] = "culture/index";
$route['messages'] = "messages/index";*/
$route['(:any)/(:any)'] = "$1/$2";
$route['(:any)/(:any)/(:any)'] = "$1/$2/$3";
$route['(.*)'] = 'welcome/index/$1';
An example of one of the controllers that will not load..
session_start();
class News extends CI_Controller {
function News()
{
parent::Controller();
}
function index() {
$data['title'] = 'Politics';
$data['main_content'] = 'news/index';
$this->load->view('shared/template',$data);
}
}
I am working on a project that has a similar requirement on those urls.
I did it by adding route like this:
$routes['news'] = 'news/index';
or
$routes['news'] = 'news';
which are exactly lines that you've commented.
Sadly, It isn't possible without those lines (at least I couldn't do it).
If your URL was: example.com/news/index, it would match the rule $routes['(:any)/(:any)'], but if it was example.com/news, it wouldn't match anything and go to your last rule.
CodeIgniter's Routing doesn't take real segments, but the uri segments shown in the url. Therefore, your url example.com/news would be interpreted as a $username = news.
You'd have to do this uri routing for each url which only has 1 uri segment. You'd want to make sure no user has the same username with your controllers or he/she can never visit the user page.
You have to write a rule in application/config/routes.php file just add a following line
$route['(:any)'] = "welcome/index/$1";
You can access by using http://example.com/username if your have added .htaccess rule for removing index.php file from url or you can access like http://example.com/index.php/username

Categories