I am new to code igniter framework. I am having issue to call view from controller.
I have 3 three controllers in my application/controllers/ folder
employee.php, home.php and dashboard.php
and 5 views in my views/template/ folder
header.php, footer.php, sidebar.php, template.php and topmenu.php
and 3 views in my main views folder
addEmployee.php, home.php and dashboard.php
I am able to hit the home and dashboard controller, but unable to hit the employee controller to load addEmployee view.
this is my addEmployee.php view
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<title>Add Employee</title>
<div class="main-content">
<?php include 'template/topmenu.php' ?>
<!-- PAGE CODE STARTS BELOW FROM HERE -->
</div>
employee.php controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
public function __construct() {
parent::__construct();
$this -> load -> model('employee_model');
}
public function index() {
$data['header'] = 'template/header';
$data['sidebar'] = 'template/sidebar';
$data['main_content'] = 'addEmployee';
$data['footer'] = 'template/footer';
$this->load->view('template/template',$data);
}
function functionToTestgetAndSaveEmployeeDetailsResult() {
$result = $this -> getAndSaveEmployeeDetails();
print_r($result);
}
}
?>
template/template.php view
<?php defined( 'BASEPATH') OR exit( 'No direct script access allowed'); ?>
<?php
$this->load->view($header);
$this->load->view($sidebar);
$this->load->view($main_content);
$this->load->view($footer);
?>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
config/config.php
$config['base_url'] = 'http://localhost:8050/test/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
I am using this url to access the views
http://localhost:8050/test/ for home
http://localhost:8050/test/dashbaord for dashbaord
http://localhost:8050/test/addEmployee for addEmployee
home and dashboard views work by this but addEmployee doesn't.
I have also tried these url but no luck
http://localhost:8050/test/employee/addEmployee
http://localhost:8050/test/index.php/addEmployee
http://localhost:8050/test/index.php/employee/addEmployee
Any idea, what is wrong with this addEmployee view? or any link ?
Seems like you might be using the wrong URL. To access the page that makes "addEmployee" the "main_content" try
http://localhost:8050/test/employee
That URL will run Employee::index()
Related
I had created login page in codeigniter and made it default controller when the application is hit but I am getting 404 page not found error when the application is hit. Please find below code of controller as main.php config and route file changes placed. My controller name is main.php and the code is as follows
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
//functions
function login()
{
$data['title'] = 'CodeIgniter Simple Login Form With Sessions';
$this->load->library('form_validation');
$this->load->view("login", $data);
}
htaccess
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
route file
$route['default_controller'] = 'main';
config file
$config['base_url'] = '';
$config['index_page'] = 'index.php';
$config['encryption_key'] = 'xRUqKhsoZ5qV6y3kqARFJFdPqJvp7X2z';
You need to set baseurl in config file
$config['base_url'] = 'localhost/cifolder/';
Then change default controller
$route['default_controller'] = 'Main';
I'm using codeigniter. How can I get last part of my URL like:
www.example.com/uk-en/category/subcategory
Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"
if url is www.example.com/uk-en/category I want "category"
if url is www.example.com/uk-en I want "uk-en"
Edit
if url is www.example.com/Home/index/uk-en/category then it's working
but What i want without class name "Home" and method "index"
Like this www.example.com/uk-en/category
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index($params=[]){
$params=func_get_args();
$last=end($params);
echo $last;
}
}
?>
routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['(:any)'] = 'Home/index';
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Use this in your routs
$route['(.*)'] = "Home/index";
This to print all routs in your controller in your index function
print_r($this->uri->segment_array());
Problem in your routes is with setting wildcard placeholder on first place. It always need to be at last place.
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
//some example routes
$route['specific/route'] = 'controller/method';
$route['specific/(:num)'] = 'page/show/$1';
//always put your wildcard route at the end of routes.php file
$route['(:any)'] = 'home/index';
Docs.
When I set $route['default_controller'] to 'blog', CI opens my controller without problems. When I want to access it via URL, CI returns a 404.
My controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Saved at
application/controllers/Blog.php
Some other config things:
$route['default_controller'] = 'blog';
$route['404_override'] = '';
$config['base_url'] = '';
URL I try to use:
http://localhost:63342/project/public_html/blog/
.htaccess to hide index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
How can I fix this issue? I tried a lot of solutions from similar questions without success
If your project is in project folder in main directory of the local server, change your address to:
http://localhost:63342/project/blog
You configured that page as your default controller, so it should work even by:
http://localhost:63342/project
How to rewrite particular url of website using .htaccess or CI routing
http://www.domain.com/user_controller/newfunction/username
as
http://www.domain.com/username
$route['/(:any)'] = "/user_controller/newfunction/$1";
or try htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]
/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";
Its easier to use CI routing system found at application/config/routes.php
$route['(:any)'] = 'user_controller/newfunction/$1';
Don't forget to set your base url at application/config/config.php
$config['base_url'] = 'http://www.domain.com/';
Add remember to remove the index.php in the url. Edit your .htaccess file at the root of your site
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
now in your user_controller, the function newfunction will look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
when you visit http://www.domain.com/nash, your code should print out nash
I am new into Codeigniter programming. I started CI by simply editing my default controller welcome. I added a function into the controller which is called on clicking a submit button. I havent done any modification in the default framework or in the config files. But upon clicking the submit button, the application returns a 404 response.
Not Found
The requested URL /login/welcome/main was not found on this server.
Apache/2.2.14 (Ubuntu) Server at localhost Port 80
The main function is what I wrote in the welcome controller. Can anyone please tell me what is wrong. My Codeigniter version is 2.1.0.
I re installed Apache and PHP. But no use still same. The code from controller and the welcome page below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('login_page');
//$this->load->view('phpinfo');
}
public function main()
{
$this->load->view('phpinfo');
}
And the welcome page is
<div id="container">
<h1>Ignitor</h1>
<div id="body">
<code>Please Login to continue. </code>
<div id="login_form">
<?php echo form_open(base_url().'welcome/main');?>
<ul>
<li>
<label>Username </label>
<?php echo form_input(array('id' => 'username', 'name' => 'Username'));?>
</li>
<li>
<label>Password </label>
<?php echo form_password(array('id' => 'password', 'name' => 'Password'));?>
</li>
<li> <?php echo form_submit(array('id'=>'submit'),'Let me in' )?></li>
</ul>
<?php echo form_close();?>
*/
Dude..
class Welcome extends CI_Controller {
public function index()
{
// echo 'Index';
$this->load->view('welcome');
// Then save the file in your application/views/welcome.php
// http://yours.com/index.php/welcome
// Or http://yours.com/welcome <-- With htaccess
}
public function main()
{
// echo 'Index';
$this->load->view('main');
// Then save the file in your application/views/main.php
// http://yours.com/index.php/welcome/main
// Or http://yours.com/welcome/main <-- With htaccess
}
}
By default, the index.php file will be included in your URL
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Have you setup the .htaccess file? It should be something like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You are skipping index.php in the URL which means you are expecting a .htaccess file to be routing the request to CodeIgniters main index.php.
Use the URL /login/index.php/welcome/main to check if your .htaccess file is faulty or not present. If you renamed or moved the directory it is in to 'login' you may have forgotten to update that.
Your .htaccess in the 'login' directory should look something like -
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /login/index.php/$1 [L]
If it was finding the main index.php and the error was due to a view/model/controller naming issue you would get an 404 from CodeIgniter, not Apache.
Did you correctly name your welcomepage view file? In other words, your controller is asking to for login_page_view.php (or at least I believe that's the default filename convention) to be in the views folder based on the line of code:
$this->load->view('login_page');