Codeigniter page routing - php

I'm new to Codeigniter and i have a problem in page routing. I have a button placed in my first page
<center>Start</center>
I have a page in my view folder. I want to direct to this page when i click my button. How can i navigate to my page in view folder on button click.
My base url
$config['base_url'] = 'http://localhost/xampp/CodeIgniter/';
Thanks in advance

As you are using codeigniter, you could simply name the function in the contorller that would load the page you want.
<center>Start</center>
or depending on your configurations:
<center>Start</center>
in the controllerName and the functionName:
// Other code ...
$this->load->view('viewFileName');

First load url_helper by
$this->load->helper('url');
Or in your autoload.php set
$autoload['helper'] = ['url'];
Then
Start
Note: Use site_url() instead of base_url(), base_url() suited
for src like
<img src="<?php echo base_url()" /> ....

First you need enable url library
$this->load->helper('url');
then test base_url(); function
Returns your site base URL, as specified in your config file. Example:
echo base_url();
And Calling view page, Suppose your file name is home.php
<?php
class Example extends CI_Controller {
function index()
{
$this->load->view('home');
}
}
?>

Your base url is incorrect
replcae
$config['base_url'] = 'http://localhost/xampp/CodeIgniter/';
to'
$config['base_url'] = 'http://localhost/CodeIgniter/';
then
<center>Start</center>
Read this tutorial for more http://w3code.in/2015/10/codeigniter-installation-beginner-guide/

Related

base_url() not loading css file

I am trying to load a view from a controller file. If I reference my class in the view everything is fine, however if I reference a method from that class my css does not load.
Here is a simple example:
Controller file:
<?php
class Example extends CI_Controller
{
public function index ()
{
$this->load->view('myview');
}
}
In the view file this works fine:
<li>EXAMPLE</li>
But, this does not:
<li>EXAMPLE</li>
I also tried to use the site_url() function but the result was the same.
Note: I have loaded the URL helper.
Have you change your .htaccess file to remove index.php in url? then
<li>EXAMPLE</li>
it will not work.
you can try to add
<base href="<?php echo cdn_url()?>" />
into the tag <head></head>
Make sure that you have configured your config to php file properly. Then See take a look and make sure that have you removed index.php from url? if yes then remove index from every url you are targeting.I Hope it will work.
In config.php
$config['base_url'] = 'http://localhost/{your_project_folder}/';

CodeIgniter button onclick(), page redirect issue

I am very new to CodeIgniter.
I have created the controller as bellow.
<?php
class Search_Student_Ctrl extends CI_Controller
{
public function index()
{
//redirect('student_home');
$this->load->view('student_home', null);
}
}
?>
My view as bellow,
<!DOCTYPE html>
<html>
<body>
<h1>Student Management System</h1>
<button onclick="location.href='<?php echo base_url();?>Search_Student_Ctrl/index'">Search Student</button>
</body>
</html>
When I click on the button I am getting bellow error.
The requested URL /sms/Student_Home_Ctrl/index was not found on this server.
Note: I have added bellow line in autoload.php
$autoload['helper'] = array('url');
Can anyone please help me how to resolve this issue?
what happens when you just go to the url in your browser?
http://yourdomain.com/sms/Student_Home_Ctrl/index
Are you still getting the requested url was not found?
Try:
http://yourdomain.com/index.php/sms/Student_Home_Ctrl/index
Did you follow the right steps to remove index.php?
There could be a few reasons for it:
it might be because you are working on a local environment and
didn't set a host in the hosts file so the base_url cannot be found by Codeigniter (see 3. or set a local host)
you didn't loaded the Helper: $this->load->helper('url');
if all of the above still don't work you should configure the base_url in your config.php: $config['base_url'] = '';
Search_Student_Ctrl/index'">Search Student
Change above lines to
<button onclick="location.href='<?php echo site_url('Search_Student_Ctrl/index');?>'">Search Student</button>
For calling controllers from view you have to use site_url() instead of base_url()
No need to add index function url. by default controller get index function
Search_Student_Ctrl'">Search Student
Please also check your .htaccess file working or not.
You have removed index.php in url? If yes then check rewrite rule is working on your server.
In codeIgniter 3 Class is always start with capital letter for controller ass well ass models.

CodeIgniter can't read other files in view folder

I'm trying to use an <a> tag to load a file in my view folder but it just won't work.
Records
I already put $this->load->helper('url'); in the function __construct() in my default controller($route['default_controller'] = "residents";)
and set $autoload['helper'] = array('url'); in my autoload.php
I can load all of my view using $this->load->view(); but cant load it using site_url()
The only files that I can load using site_url() are the files in the root directory of codeigniter.
Can anybody tell me what should I do in order to make site_url() load my view files? Thanks
your question is not clear but its a guess
use base_url() instead of site_url().Hope it will work.

CodeIgniter controller to view to another controller url

Say for example, I loaded a view from controller1
The url should be index.php/controller1/<function where view is ran>
Then from that view I have a link.
<a href='controller2/view'>View</a>
After clicking that link, the url now looks like this.
index.php/controller1/controller2/view
Is there a way to clear previous url_segment, or is there a better way to link to another controller's function.
Do.
<?=base_url(); ?>/controller2/view
Inside your href.
You can write the link as
<a href='<?=base_url(); ?>controller2/view'>View</a>
remember that you have set the base_url in your config/config.php file
$config['base_url'] = 'http://yourSite';

Iframe Access PHP File In Codeigniter

i want to make iframe in codeigniter which access a php file in my local folder. Like this
<iframe src="assets/kcfinder/browse.php?type=files&dir=/assets/admin/images" frameborder="0" ></iframe>
But the display always redirect it to 404 page. How can i fix it?
Thank you very much.
If you're using CI you are ill-advised to call a php file directly. Create a controller instead and call that instead. Something like:
<?php
require_once dirname(__FILE__) . "/base.php";
class fck extends BaseController {
function __construct() {
parent::__construct();
$this->load->model('myModel');
}
function browse($type,$dir) {
$data['directory'] = $this->myModel->loadDirContent($type,$dir);
$this->load->view("fck/browse", $data);
}
}
?>
Your iframe then looks like this:
<iframe src="/fck/browse/files/assets/admin/images" frameborder="0" ></iframe>
Note that after 'browse' in the URI above you are effectively submitting data. First what is read out as "$type" by the browse function, i.e. "files" then the path itself, i.e. data that contains dashes. This is no longer part of an URL so to speak.

Categories