Codeigniter - uri segment - codeigniter

i need to create an url like this: www.example.com/index.php/scheda/name-surname-id.html
so i create the Scheda controller, this is the code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheda extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$name_scheda = $this->uri->segment(2);
//i need only the id for the search into db
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}
but when i write the url in the address bar i get an 404 error...can someone help me to understand why?

Your url:
www.example.com/index.php/scheda/name-surname-id.html
Should be:
www.example.com/index.php/scheda/index/name-surname-id.html
index() is the default method, but the index segment can only be missing from the URL if there are no arguments, otherwise Codeigniter will think you are trying to call the method name-surname-id.html().
You can use routes.php or _remap() to clean up the URL and remove the index segment.
// routes.php
$routes['scheda/(:any)'] = 'scheda/index/$1';
OR:
class Scheda extends CI_Controller{
function _remap($method, $args) {
$name_scheda = $method;
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}

Related

How to redirect old php routes in codeigniter

I have old urls like http://example.com/products.php?s_productid=231 and now in my proyect whit url would be http://example.com/products/ver/231
How can I make a rule in routes.php to redirect?
Thanks
you don't need to do any change in routes.php, all you need is to create a Controller "Product", with one Method "ver", which receives 231 (it's id I guess) as a parameter, like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
// do your things
}
I hope it helps.
You may need to set the routes in config/routes.php
$route['product/ver/(:num)'] = 'product/ver/$1';
http://example.com/index.php/products/ver/231
Then the controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
/*
echo $id;
*/
}

Fatal error: Call to a member function get_tab() on a non-object in D:\wamp\www\relation\application\controllers\c_relation.php on line 7

I got this problem, I researched more but didn't find any solution .
I am creating a Database relation logic For Crud .
Model code is :
<?php
class M_relation extends CI_Model{
public function __construct()
{
$this->load->database();
}
public function get_tab(){
$query = $this->db->get('user_id');
return $query->result_array() ;
}
}
And Controller Code is :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_relation extends CI_Controller {
public function index(){
$this->load->model('m_relation');
$data['user_id'] = $this->M_relation->get_tab();
echo var_dump($data['id']);
$this->load->view('v_relation');
}
public function __construct()
{
parent::__construct();
$this->load->model('m_relation');
}
}
?>
Please :)
$query = $this->db->get('user');
Instead
$query = $this->db->get('user_id');
The mistake was that i set the get() to user_id its a table field .
instead of table name .
please replace
$this->load->model('m_relation');
to
$this->load->model('M_relation');
You load model as m_relation but when you access function using M_relation.

Codeigniter redirect to rigister(404) page

class Welcome extends CI_Controller{
function checker(){
if (!$_SESSION['bla']) {
$data['include'] = "REGISTER"; //first result
}else{
$data['include'] = "template";
}
$this->load->view('template', $data); //second result
}
//STOP HERE IF RESULT IS FIRST
function index(){
$data['include'] = "index";
$this->load->view('template', $data);
}
}
I want if in my page dont have session all user redirect to 'REGISTER' page, all classess, all functions to redirect in REGISTER page if dont have session. BUt if user has session just can use site normal. HOw to do this ?
I dont want in every class to put this check ... THanks
Create a controller MY_Controller in application/core/MY_Controller.php and extend the CI_Controller like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
function __construct() {
parent::__construct();
// do the checking here...
// if condition fails redirect...
}
}
Extend this MY_Controller class in your every controller instead of CI_Controller so in every request the checking would take place at first. For example:
class User extends MY_Controller {
// ...
}

Redirect loop while extend controller Codeigniter

I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.
As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){
lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)

codeigniter : load second controller after execution of first controller

we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).

Resources