Edit: I am new Codeigniter I am not how to use Codeigniter Routing. I create Contact Us page and Map page. Map page is the subpage of Contact Us page.
Table Name : Pages
id label link parent
1 Contact Us contact-us 0
3 About Us about-us 0
2 Map map 1
Here my Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('page_model');
}
public function index()
{
$data['getAllPage'] = $this->page_model->getAllPages();
$this->load->view('page_listing',$data);
}
public function view($id) {
$data['single_page'] = $this->page_model->displaySinglePage($id);
$this->load->view('single_page',$data);
}
}
In routes.php I have put $route['(:any)'] = "page/view/$1";
When I enter url "http://mytest.dev/contact-us/" or "http://mytest.dev/about-us/" it show correct content of Contact page but I enter "http://mytest.dev/contact-us/map" it still show content of Contact page.
What I want when I enter "http://mytest.dev/contact-us/map" it shuold show content of Map page
Thanks in advance.
Please try code maybe can help
// Parents and Child page.
$route['page/(:any)/(:num)'] = "page/view/$1/$2";
// For Main Home Page.
$route['(:any)'] = "page/view/$1";
I think following routes should work
$route['contact-us'] = 'page/view/$1';
$route['contact-us/map'] = 'page/view/$2';
when you'll echo $id in view method.
"http://mytest.dev/contact-us/" will print $1
and
"http://mytest.dev/contact-us/map" will print $2
Hope this solution will help.
My site has a main 'events' page and an 'events/calendar' page. Create the pages as separate views and then create a function for each sub page in the main controller. Here's my Events controller. If you want a dynamic page you can add the appropriate code in the function.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Events extends CI_Controller {
public function index()
{
$this->load->view('inc/header.php');
$this->load->view('events_view');
$this->load->view('inc/footer.php');
}
public function calendar()
{
$this->load->view('inc/header.php');
$this->load->view('calendar_view');
$this->load->view('inc/footer.php');
}
}
Related
I'm developing a web application and i'm slightly confused by routes and how they work.
My web application has an admin area and the URL structure is as follows;
example.com/admin/view/form/123
My Admin controller looks like this;
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function index()
{
$data = array(
'title' => 'Admin Page'
);
$this->load->view('admin/index', $data);
}
public function view() {
$form_submission_id = $this->uri->segment(4);
$records = $this->Admin_model->getDetails($form_submission_id);
$data = array(
'title' => 'Form Details',
'records' => $records
);
$this->load->view('admin/view/index', $data);
}
}
I don't have any custom routes setup.
When I visit the following URL, I can see the page and corresponding data successfully;
example.com/admin/view/form/123
But, when I change the /form/ URL segment to something random like below I can still see the correct data;
example.com/admin/view/foo/123
Why is this?
I was expecting to see a 404 page?
What do I need to change in order to achieve what I want?
Perhaps i'm misunderstanding the logic and should have my controllers / routes setup differently?
Codeigiter URL has a structure as domain/controllerName/actionName/param1/param2 and so on. In your code URL example.com/admin/view/form/123 admin is controller, view is action name and form and 123 is the parameters which you passed using get method. You can access these parameters like $this->uri->segment(3).
Thus in your code:
It will not show any error as your function is not even using 3rd URI segment.
It will not show 404 page as it found correct controller and action.
To achieve domain related functionality, you need to either change for function code accordingly or need to use routes for this.
Hope it helps you to clarify this code.
Rohit Mittal answer is good and also,
You can change the view fuction in admin controller like as:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function view($form = null,$form_submission_id = null) {
if($form == "form" && $form_submission_id){
$records = $this->Admin_model->getDetails($form_submission_id);
$data = array(
'title' => 'Form Details',
'records' => $records
);
$this->load->view('admin/view/index', $data);
}
}
I have some controllers: Post, Pages, Authors. On each controller, I want to set the individual URL from the database. The structure of the database page: There will be thousands of records in the database.
how can this be implemented, take also indicate every URL will load from the database on the basis of slug. I stuck in this from last two day
Current Url structure is -
http://127.0.0.1/hmvc/post/post_details?id=1
I want urls something like this
http://127.0.0.1/hmvc/blog-post-1
Since you have the slugs already in your database I'm assuming that you already have the CRUD of that table done and you just want to interact with it.
First your controller and method:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post extends CI_Controller {
public function post_details($slug)
{
$this->load->model('article_model', 'article');
$this->data['article'] = $this->article->get_by_slug($slug);
}
}
/* End of file post.php */
/* Location: ./application/controllers/post.php */
Then your model:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Article_model extends CI_Model
{
public function get_by_slug($slug = null)
{
if (is_null($slug)) {
return array();
}
return $this->db->where('slug', $slug)
->get('posts')
->row();
}
}
/* End of file article_model.php */
/* Location: ./application/models/article_model.php */
Finally your routes should look like this:
$route['default_controller'] = 'dashboard';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['(:any)'] = 'post/post_details/$1';
Please check the following code by placing it at the bottom of your config/routes.php file.
What it does is, check if 'blog-post-' is present in the uri(not in querystring) part. If present, then explode it and check if the second part is a valid positive integer. If yes, then set the route rule for 'post/post_details/{NUMBER}' for the uri.
It will not break the routes rules for other controllers(Pages, Authors) by trying to redirect their hits to 'post' controller.
$uri = $_SERVER['REQUEST_URI'];
$check_part = 'blog-post-';
if (strpos($uri, $check_part) !== FALSE) {
$uri_parts = explode('blog-post-', $uri);
if (count($uri_parts) == 2) {
$id = intval($uri_parts[1]);
if ($id > 0) $route[ltrim($uri, '/')] = 'post/post_details/'.$id;
}
}
Here is what I want to achieve:
https://www.example.com/properties
https://www.example.com/properties/properties-in-newyork
https://www.example.com/properties/properties-in-DC/property-for-rent
https://www.example.com/properties/all-cities/property-for-rent
https://www.example.com/properties/all-cities/property-for-sale
All above is for search. Now I want to get details page like:
https://www.example.com/properties/2br-apartment-for-sale-100
I want to differentiate between search and details page links. Here is what I tried:
$route['properties/index'] = 'properties';
$route['properties(/:any)'] = 'properties/property_details$1';
How can I differential which URL is for properties/property_details function and which URL is for properties/index function?
enter image description here
Set your route.php like this :
$route['properties/index'] = 'properties';
$route['properties'] = 'properties/property_details';
$route['properties/(:any)'] = 'properties/property_details/$1';
Access url :
this direct you index method
https://www.example.com/properties/index
this will direct you property_details method
https://www.example.com/properties/
Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends CI_Controller {
public function __construtct()
{
parent::__construtct();
$this->load->helper('url');
}
public function index()
{
echo 'index';
}
public function property_details($component = NULL)
{
echo 'property_details';
echo $component;
}
}
If I'm correct, according to your explanation with differentiating the routes, the problem you are having is, it always running the route for index despite of what your URL having after properties.
You may try it by changing the order of the routes like this;
$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index'] = 'properties';
It always works according to the order of the routes you have placed. If there are acceptable parameters, for the program, properties/index is also something similar to properties(/:any). So, to differentiate between these two, we have to change the order of the routes like this.
i have a login process where the user can view his dashboard after login.
The code in controller:
$adminid = $this->am->login_admin($email, $password);
if ($adminid) {
$admin_data = array(
'adminid' => $adminid,
'email' => $email,
'logged_in' => true,
'loggedin_time' => time()
);
$this->session->set_userdata($admin_data);
$this->session->set_flashdata('login_success', 'You are logged in');
redirect('Admin_dashboard/dashboard/' . $adminid);
} else {
$this->session->set_flashdata('login_failed', 'Invalid login!!');
redirect('admin/index');
}
After successful login the user is getting redirected to the following url
localhost/project/Admin_dashboard/dashboard/1
The issue is that if the user manually changes the url to something like this-
localhost/project/Admin_dashboard/dashboard/2
he is able to access the data of user whose id is 2 without login
To solve the issue i tried using the following codition in the view
<?php if($this->session->userdata('logged_in')): ?>
<? endif; ?>
However the 2nd url is still accessible
After login the user gets redirected to dashboard that also contains few other pages such as profile page, payment page etc which contains data that is only related to him.
I want that after login he should be able to see all his pages but not anyone else data by changing the url
Simply do one thing, instead of passing $adminid with the url, get the adminid with session, because you also storing values in session.
Instead of
redirect('Admin_dashboard/dashboard/' . $adminid);
Use this
redirect('Admin_dashboard/dashboard');
and inside the dashboard function in Controller use this
public function dashboard (){
$admin_data = $this->session->userdata('admin_data');
if(!isset($admin_data['adminid']) || empty($admin_data['adminid'])){
//Error message Login First
redirect('admin/index');
}
$adminid = $admin_data['adminid'];
//Proceed with this $adminid
}
Simply add this code to all controllers for maintaining user restrictions throughout all URLs.
Class Controller_name extends CI_Controller{
function __construct(){
parent::__construct();
if(!isset($this->session->userdata['logged_in'])){
//redirect login page
}
}
/**
Your Other Functions
**/
}
Let me know If you have anymore doubts..
set user session is valid or not in dashboard controller before load dashboard view and also check user session adminid value with uri segment value
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('Login', 'refresh');
}else{
$uri_admin_val=$this->uri->segment(2);
$adminid=$this->session->userdata('adminid')
if($adminid!=$uri_admin_val){
redirect('Admin_dashboard/dashboard/' . $adminid);
}
}
}
}
And extend this my controller on dashboard and other controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Dashboard extends MY_Controller {
public $data;
public function __construct() {
}
}
I have an home controller who control the homepage (that is a simple landing page with no user interaction or dynamic data):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
private $data;
protected $pagedata;
function __construct()
{
parent::__construct();
if ($this->session->userdata('is_logged_in') == true) {
$this->data['nav'] = 'auth/template/homelogin_nav';
}
$this->pagedata['title'] = 'La Giumenta Bardata Cosplay & Props';
}
/**
* Index Page.
*
**/
public function index()
{
$this->load->view('template/header', $this->pagedata);
$this->load->view("template/nav", $this->data);
$this->load->view('section_header');
$this->load->view('section_about');
$this->load->view('section_services');
$this->load->view('section_portfolio');
$this->load->view('section_social');
$this->load->view('template/footer');
}
}
So basicly, if user is log in I load a certain view that correspond to a nav, if not it's loaded the normal menu.
Now, the two navs are different just for one link (one nav's view has a link to the login page and the other one has a link to the user dashboard).
I also try this:
$this->load->view($this->data);
but of course is illegal and it doesn't work.
The problem starts because I have to check for the session in the costruct and not inside the function index() or I can't check it.
Why don't you check for session inside of view?
Make one navigation and if user is logged in show dashboard, if not show login button in navigation.
Navigation view:
<ul>
<li> <?php echo anchor('home','Home'); ?> <li>
<?php if($this->session->userdata('is_logged_in')== true){
echo "<li>".anchor('dashboard','dashboard')."</li>";
echo "<li>".anchor('logout','logout')."</li>";
}else
echo "<li>".anchor('login','login')."</li>"; ?>
</ul>