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}/';
Related
I have a project in laravel5 and I have directed virtual host on /public directory. Where should be my main page index.php now? There is some index.php in public folder already but it contains some content that I dont understand. So where should be my index now? Maybe in views?
If you are using Laravel 5.1 then your initial page should be this resources/views/welcome.blade.php.
You can change it to any name. But also you should change the same in your Controller.
Note : If you are rendering the view through controller then you should have the file name like this yourfilename.blade.php
Your views should always inside resources/views/*
The index file stays at the same place, to call a new file that you made, could be with HTML, you can put that file in the view and call it from the controller, hope this is the answer you are looking for.
When a request come to your server, the first file that it is executed is index.php. But this is not the file shown. In Laravel you don't have to force any file be named index.php. So let's imagine that you are trying set up a new index file. You have to use routes.php
routes.php
Route::get("/" , "HomeController#index");
HomeController.php
function index(){
return view("home/index");
}
views/home/index.blade.php
<html>
<head>
</head>
<body>
<p>Index page</p>
</body>
</html>
When a GET request to "/" it's done, the index function of the HomeController it's executed and the view index.blade.php, stored in views/home it's shown.
This it's the basic behaviour of Laravel. So you mustn't rename or move the index.php file in public folder.
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/
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.
<script src="<?php echo site_url('bootstrapper/jqTimer'); ?>"></script>
So I've tried to call the controller below using the script above but I get error not found. Any ideas as to why this method is not working?
function jqTimer() {
$this->load->view("jquery.timers.js");
}
When loading javascript with a <script> tag, the src attribute is expecting a file name to a js file. You're giving it a path to a controller method in your CI install.
What you need to do is put the jquery.timers.js file in the public_html folder and access it from there:
// assuming you have the script in a [javascripts] folder inside [public_html]
<script src="<?php echo site_url('javascripts/jquery.timers.js'); ?>"></script>
If you'd prefer to load your javascript through views, then you need to do this instead:
<script><?php echo $this->load->view("jquery.timers.js", "", TRUE); ?></script>
This will echo out the contents of the view file between the <script> tags for embedded javascript. (passing TRUE as the third parameter returns the content of the file, so you can echo it out)
I don't see any reason why you should create a function in the controller just to load a javascript file.
The base_url() function can already do what you want.
<script src="<?php echo base_url('path/to/jquery.timer.js'); ?>"></script>
Have a look at the documentation for the URL helper
You're looking to echo out a link to the js, not echo out the js itself.
You could create a MY_Controller in the core folder which extends CI_Controller, in which:
function jqTimer() {
return site_url("jquery.timers.js");
}
Then, in any other controller (which extends MY_controller) you'd go:
$data['js_timer'] = $this->jqTimer();
/* proceed to load main view with $data */
View:
<script src="<?php echo $js_timer; ?>"></script>
This is DRY in that, should you decide to use a different js timer plugin, you need to replace it in one place only: the MY_Controller class (as opposed to every view file).
Still, a bit of an odd way of doing things...
As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.
Following instruction in the docs, I load the library in my controller:
$this->load->library('javascript');
Then, I define the location of the jQuery file (jquery.min.js) in the config.php:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
After that, I open the view file and put in these two lines:
<?php echo $library_src;?>
<?php echo $script_head;?>
First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)
Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:
$this->load->library('jquery');
Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)
Checking on system folder it looks all files are in place:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
Thanks in advance for your help!
Put the code in the config.php like this:
$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';
In your controller file (e.g. controllers/sample.php) type this codes:
function __construct()
{
parent::__construct();
$this->load->library('javascript');
}
function index()
{
$data['library_src'] = $this->jquery->script();
$data['script_head'] = $this->jquery->_compile();
$this->load->view('sampleview', $data);
}
In your view file (e.g. views/sampleview.php) type this codes:
<?php echo $library_src;?>
<?php echo $script_head;?>
This works for me. I hope it works for you too. XD
It is important to note that this Driver is marked as experimental so I wouldn't rely on it.
Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.
To use javascript in your views, I would just start out by loading them like this...
<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>
Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.
First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not $script_head but in fact$script_foot.
Second, once you've finished making your calls, it seems you need to run
$this->javascript->external();
and
$this->javascript->compile();
These functions set the $library_src and $script_foot variables.
To put it all together, in your controller you would have:
class Some_Controller extends CI_Controller {
public function index()
{
$this->javascript->click('#button', "alert('Hello!');");
$this->javascript->external();
$this->javascript->compile();
$this->load->view('index');
}
}
In your view you would have
<html>
<head>
<?php echo $library_src; ?>
<?php echo $script_foot; ?>
Although CI first looks for system folder, you can also try putting your libs in the these folders:
application/libraries/Javascript.php
application/libraries/javascript/Jquery.php
Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.
As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .
Put the following line :
// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'
$config['javascript_location'] = 'js/yourfile.js';
Second step is open your controller, within constructor method, put the following code :
// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery
Then, still in controller file within index method :
$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().
Then open your view file and put the following code within tag head :
<?php echo $library_src; ?>
That way is working for me, let's try it.
Use:
$this->load->library('javascript/jquery');
instead of:
$this->load->library('jquery');
This will load your jQuery library.
I had the same problem, and found :
<?php $this->load->library('javascript'); ?>
<?php $this->load->library('javascript/jquery'); ?>
on https://ellislab.com/forums/viewthread/181742/#860506
Because jquery is in javascript folder.
Or
$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');
In the autoload.php file.
That solved the problem of loading the librairy.
You can try this, it's work to me.
in config.php
$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
in Controller
public function __construct() {
parent::__construct();
$this->load->library('javascript');
$this->load->library('javascript/jquery');
}
public function index(){
$d['library_src'] = $this->jquery->script();
$d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
$this->load->view('main',$d);
}
in view (head)
<?php echo $library_src; ?>
in view content (body)
<div class="alert alert-dismissable alert-info" id="logalert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
</div>
in javascript view
<script type="javascript">
$(document).ready(function(){
<?php echo $logalert; ?>
};
</script>
happy coding :)