url shortning in code in code igniter - codeigniter

my controller `
class index extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('login');
}
` public function home()
{
$this->load->view('header');
$this->load->view('index');
$this->load->view('footer');
}
}
the url of function home in this controller is this
http://localhost:88/mlmm/index.php/index/home
i want this url to be
http://localhost:88/mlmm/home

To remove index.php from your URL you need to add .htaccess file and below code inside that.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
for making url like this http://localhost:88/mlmm/home you need to mention routes in your config/routes.php file
$route['home'] = 'index/home';
Note : you have mention the controller name index and in codeigniter by default controller first method is also index so this can be make problem so I suggest use different controller name.

Related

How to call many function with one controller file?

How to removed url index.php in codeigniter ?
My output display :- http://localhost/shukla/index.php/home/about
And i tried to get result display:- http://localhost/shukla/home/about
http://localhost/:server_name/shukla/:folder_name/index.php/home/about/:about_page
I create a CodeIgniter, controller file--> Home.php
class Home extends CI_Controller {
public function index()
{
$this->load->view('template/header');
$this->load->view('home');
$this->load->view('template/footer');
}
public function about()
{
$this->load->view('template/header');
$this->load->view('about');
$this->load->view('template/footer');
}
public function contact()
{
$this->load->view('template/header');
$this->load->view('contact');
$this->load->view('template/footer');
}
}
In view folder: home.php , about.php, contact.php
config.php set base_url $config['base_url'] = 'http://localhost/project/';
removed index.php : $config['index_page'] = '';
In config.php there is an option "index_page" make it blank.
path : project_folder->application->config->config.php
change
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';

CSS is not getting applied in function call of codeigniter

In my dashboard all the css and js are getting applied, but when i call a function from other controller then the css and js will not get applied.
My code from view is - File sidebar.php:
<li><i class="fa fa-circle-o"></i> Add Farm</li>
And controller is - farm.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Farm extends CI_Controller {
public function index()
{
$this->load->view('farm_info');
}
public function add_farm()
{
$this->load->view('farm/add_farm');
}
}
Also my .htaccess file contain
# If your default controller is something other than
# "welcome" you should probably change this
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ /index.php/$1 [L]
Please help me with this, i will provide you any other info if you want

Codeigniter - cannot load any view at all

I have built a website with the CodeIgniter framework.
Unfortunately, I am having a problem with CodeIgniter's load->view() function.
Every time I try to load another view file with the code $this->load->view('any_view_file'); I get the error:
unable to load the requested file 'any_view_file'.php
For example i am having problems with the $this->load->view('available_lessons.php', $data); command. So let's try to recreate the problem and think this through...
I am calling the load_available_lessons() function of the Lessons.php controller
The load_available_lessons() function uses the get_lessons() function of the db_model.php model and saves the result on the $data['result'].
Then, the load_available_lessons() function calls the available_lessons view file while parsing the $data to it.
Here is where the problem rises. CodeIgniter is unable to load the view. As i explained earlier, CodeIgniter fails to load any view...!!!
Any suggestions please?
My .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\\.php|images|css|lessons|robotos\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
From my config.php file:
$config['index_page'] = '';
From my routes.php file:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = TRUE;
My Pages.php Controller:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
}
?>
My Lessons.php Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lessons extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('db_model');
/* $this->load->library('acl'); */
/* $this->load->helper(array('form', 'url')); */
}
public function index()
{
$this->load->view('available_lessons', array('error' => ' ' ));
}
public function load_available_lessons()
{
$data['result'] = $this->db_model->get_lessons();
$this->load->view('available_lessons.php', $data); /* available_lessons */
}
}
?>
My db_model.php Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class db_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
public function new_lesson($data)
{
/* Inserting in Table "lessons" of CodeIgniter's Database */
$this->db->insert('available_lessons', $data);
}
public function get_lessons() /* $data */
{
/* Get lessons from Table "lessons" of CodeIgniter's Database */
$this->db->select('Name, Description, DateAdded, LastEdit, UserID, FileName');
$this->db->from('lessons');
$query = $this->db->get();
return $result = $query->result();
}
}
?>
My available_lessons.php View file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="container">
<h6 class="description">Browse Lessons</h6>
<div class="row">
<section id="lessons-panel" class="col-xs-12 col-md-8 col-md-offset-2">
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($this->result as $r): ?>
<tr><?php echo $r->content; ?>
</tr>
<?php endforeach; ?>
</table>
</section>
</div>
</div>
As #ourmandave and a coworker pointed out to me, i should add the "pages/" before every view name that i want to call. For example, in the Lessons.php controller, i should have $this->load->view('pages/available_lessons.php', $data); As it turned out he is right.
The thing is that i cannot understand the reason why i should include the "pages/" as I have already included this in the Pages.php controller. It seems that the Pages Controller is not being used when i call a view. I will have to look into it later on.
Thank you all for your valuable input!!!!! Just another rookie mistake...

Unable to load your default controller

I have a Codeigniter Project where i add this to my routes.php File
Routes.php
$route['default_controller'] = "site/landing";
$route['404_override'] = 'site/landing/page_not_found';
My directory structure is as
controllers --- site -- landing.php
Landing.php
class Landing extends MY_Controller {
public function index(){
# Some Code Here
}
public function page_not_found(){
#load 404 page Here..
}
}
My_Controlelr.php
class MY_Controller extends CI_Controller {
#Some COde Here..
}
Now when i go to some non existing URL like mydomain.com/ajsdhajksbcasbkjakjghdakjsdh
I get this error
Please make sure the controller specified in your Routes.php file is
valid.

Codeigniter login redirect looping error

After i had changed my codeigniter files to another hosting it come out with this error.
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects
It might cause by the infinity looping redirect in my code but i cant figure it out whats the error in my code
my route default controller is inside.php
here is the inside.php (controller) code
class Inside extends Controller{
function __construct(){
parent::Controller();
$this->loginchk_model->is_logged_in();
}
function index(){
$this->template->write('title', 'Update to date data');
$this->template->write_view('header', 'header_content', true);
$this->template->write_view('content', 'inside_view', true);
$this->template->load();
}
}
and here is the loginchk_model.php (model) code
class Loginchk_model extends Model{
function is_logged_in(){
//check session exist or not
if($this->session->userdata('is_logged_in') && $this->session->userdata('username')){
redirect('inside');
die;
}else{
redirect('login');
//echo anchor('login','Back to login page');
die();
}
}
}
and here is the login.php (controller) code
class Login extends Controller{
function __construct(){
parent::Controller();
$this->load->helper('form');
$this->load->model('his_action_model');
}
function index(){
$data['title'] = 'Login in';
$this->load->view('login_view', $data);
}
here is my htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /foldername/index.php/$1 [L]
</IfModule>
Stuck for few hours in this problem.. i am also wondering why changed hosting cause such problem.
thanks in advanced!
That issue could be because of your .htaccess I believe I had that issue before when going from local server to my host. There was extra lines at the bottom of the .htaccess that were causing an issue on that particular server so I commented them out. But I could be wrong.
Is this happening for just you or have you tried viewing the error from a different PC?

Resources