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
Related
I am using CodeIgniter. I have frontend and backend folder inside controllers and views. I tried server steps even check almost all the solution but still I am not able to access it my default controller
routes.php
$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
/*backend*********************************/
$config['backend'] = 'backend/Access_control';
1) My issue is When I am accessing the Url http://localhost/example_ci_row/
I am getting 404 Page not found
2) How to access the backend URL I tried http://localhost/icube_row/admin
but I am getting the error
frontend User_control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
Backend Access control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Access_control extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('backend/login');
}
}
?>
Edited
It's working when I use below steps. I added the Test.php file in the controller and change the routes then I am getting the login page.
Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
routes.php
$route['default_controller'] = 'Test';
Hope this will help you :
The built in $route['default_controller'] will not work for sub-folders. you have to extend system router as per your requirements like this :
You need to create a MY_Router.php in application > core > MY_Router.php
<?php
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
This will allow you to use $route['default_controller'] = 'frontend/User_control'; as your default controller
Have you configured the Base URL ?
Application->config->config.php
$config['base_url'] = 'http://localhost/example_ci_row';
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.
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)
I'm working on a client website which part of it to retrieve news, this is my first codeigniter application and I have followed CI tutorials, slugs and routing.
The problem I'm having is that everything works fine but when it comes to get a record based on slug I get 404. What I did was removed index.php from my URL and tested it, which works fine.
This is my route.php
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['news/(:any)'] = 'news/singe_news/$1';
$route['news'] = 'news';
This is my model news_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_model extends CI_Model {
function get_all(){
$sql="SELECT * FROM news AS news";
$query=$this->db->query($sql);
return $query->result();
}
//// get a single post
function get_single($slug = FALSE) {
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
function news_categories(){
$sql="SELECT * FROM news_categories";
$query=$this->db->query($sql);
return $query->result();
}
}
and this is my controller news.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
public function index()
{
$this->load->model('news_model');
$data['results']=$this->news_model->get_all();
$this->load->view('public/header', $data);
$this->load->view('public/menu');
$this->load->view('public/news-listing', $data);
$this->load->view('public/footer', $data);
}
public function single_news($slug) {
$this->load->model('news_model');
$data['single_news'] = $this->news_model->get_single();
/// if (empty($data['single_news']))
/// {
/// show_404();
///}
$this->load->view('public/header');
$this->load->view('public/menu');
$this->load->view('public/single-news', $data);
$this->load->view('public/footer');
}
}
So questions I have:
1- Where does $slug come from? I have a view has
2- Is there anything missing?
URLs need to be
domain.com/news/this-is-a-slug
Many thanks and apologies if this may have been asked in different format with different intention on other posts.
Where does $slug come from?
Your URI looks like this: www.example.com/controller/method/arg1/arg2/arg3 (w/o query string)
Is there anything missing?
Well, there is few things you should do:
Use autoload (config/autoload.php) to load your most used models, or if this model is not used widely at least load it at class constructor.
You are not passing $slug argument to model method, this will not work even if you will fix your routing.
$data['single_news'] = $this->news_model->get_single();
Better to show 404 if you can't find a slug, don't refetch all data on fail.
To fix 404 error follow those steps:
Check your .htaccess file for errors
Check uri_protocol setting in config
Try routing like this $route['news/((.+)$)?'] = "news/singe_news/$1"; (this should replace both routes for news)
How can i "split" a releated url with a - ?
So i`ve a URL like "domain.de/thisismytest"
But i want to have "domain.de/this-is-my-test" ?
Currently i`ve this in my routes.php
$route['this-is-a-test'] = "site/thisisatest";
In my really simple Controller i`ve this Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function thisisatest() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_thisisatest");
$this->load->view("site_footer");
}
public function about() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
Your configuration seems to be correct. This routing config line:
$route['this-is-a-test'] = "site/thisisatest";
remaps to the Site class and the thisisatest method.
Are you sure that you do not have this routing in config:
$route['thisisatest'] = "site/thisisatest";
? In that case both would point to the same method.
Anyways I think that the problem is that you're referencing the thisisatest URLs somewhere in your views. Check if your not calling site_url('thisisatest') or base_url('thisisatest') or simply domain.de/thisisatest somewhere in the views and replace it with site_url('this-is-a-test').
In your index function you could grab the url segment and processes the string:
$segment = $this->uri->segment(1); // the url segment you need
$segments = explode( '-', $segment );
$yourFunction = implode ( $segments );
check if the function exists
if(method_exists($this,$yourFunction))
$this->$yourFunction();
and in your routes file point to your index:
$route['yourController/(:any)'] = 'yourController/index';