I am new to Cake PHP and I had a website with regular PHP and HTML, but now I am switinching to use Cake PHP. The issue I am having is that it is not picking up the javascript for a slider I have on a menu. (But it works on the original web).
So I did the following steps, maybe you can see where I am making my mistake.
First I put the Javascript file under webroot/js
FILE javascript.js
/**
*
*/
$(document).ready(function() {
//ACCORDION BUTTON ACTION
$('div.accordionButton').click(function() {
$('div.accordionContent').slideUp('normal');
$(this).next().slideDown('normal');
});
//HIDE THE DIVS ON PAGE LOAD
$("div.accordionContent").hide();
});
Then I went to AppController and added the following line under cake/libs/controller/app_Controller
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript');
}
Then, that change allowd me to add it to layout
FILE mylayout.ctp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<?php
$javascript->link('javascript.js');
</head>
<?php include 'menu_nav.php';?> //this has starting <body> tag
<?php include 'leftmenu_nav.php';?>
<div id="content" class="divContent">
<div id="mainContent" class="divMainContent" style="text-align: left;">
<?php echo $content_for_layout; ?>
</div>
</div>
<?php include 'footer.php';?> //this had end </body> tag
</html>
Now somewhere in the
$content_for_layout
there some <divs> that make use of the JS, but for some reason 1) I do not see the JS markup when i do page source and 2) It is not sliding like it used to..
Can anyone please tell me what I may be missing in this CakePHP config?.
Thank you
echo $this->Html->script('javascript.js');
echo $this->Html->script('jquery-ui-1.8.16.custom.min.js');
echo $this->Html->script('jquery.js');
echo $this->Html->script('jquery.MetaData.js');
echo $this->Html->script('jquery.min.js');
echo $this->Html->script('jquery.rating.js');
echo $this->Html->script('jquery.rating.pack.js');
echo $this->Html->script('jquery.validate.js');
echo $this->Html->script('paging.js');
echo $this->Html->script('ui.datetimepicker.js');
echo $this->Html->script('format.js');
There are a couple of things you need to check.
First, don't change anything in the cake folder. That is a big no no. If you want to have an app controller, simply create a new file name app_controller.php and put it in your /app folder. Add the following code in it.
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript'); //what version are you using?
}
How you include the JS file depends on what version of cake your are using. The newer version uses the following syntax
<?php echo $this->Html->script('script'); ?>
http://book.cakephp.org/view/1589/script (1.3)
Next, make sure that all the JS is actually being included. Look at the source and click through to the JS file to confirm that the file is loaded. I don't see a jquery include anywhere so you should confirm that jquery is included in your layout.
After that, confirm that the div.accordionButton div is contained in your view. Since it doesn't look like it's in the layout, open up the specific view element and check in there.
As a last resort, add alert($('div.accordionButton').length) in your JS file to confirm that the JS is actually picking up the DIV from the view. If the alert shows 0, then this means that it's not.
You'll want to use this format instead, I insert it in my layout.ctp.
echo $this->Html->script('jquery.min.flip.js');
I think there are different ways of calling js files, but I keep them in /app/webroot/js and use this call to load the file.
Related
At the very top of my page/site, before <!doctype html> etc., I load my classes with spl_autoload_register().
One of the classes is site, and inside this class I have a static function:
<?php
/**
* A fast and easy way to include content to a page...
* "dir_pages_contents" is a defined constant with the path to the content files
*/
public static function include_content($content_name){
if(file_exists(dir_pages_contents.$content_name.'.cont.php')){
include dir_pages_contents.$content_name.'.cont.php';
} else {
/* echo an error message */
}
}
?>
I was hoping to do something like this i practice:
create a new document with some content and save it with the extension .cont.php into a folder specified for page contents.
Then; On the page where I want this content to be displayed - I do:
site::include_content('test_doc');
This almost works; The document, or content, is included and displayed.
But it seems like it is included where the class is - at the very top where the class is - because PHP-variables set outside of this document is not available within the document at all.
Here's a illustration of the setup:
test_doc.cont.php
<?=$hello?>
index.php
<!-- PHP-classes are included here -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<?php $hello = 'Im outside...'; ?>
<!-- this includes the document, but the variable $hello is not -->
<?=site::include_content('test_doc')?>
<!-- this works perfectly -->
<?php include dir_pages_contents.'test_doc.cont.php; ?>
</body>
</html>
The inclusion of a separate file, or document, is done immediately when the include-statement is read by the script i guess? but not displayed until futher down the script where the function was called?
Any suggestion for another way to accomplish this?
I'm not looking for any MVC or other PHP-framework.
EDIT:
user1612290 pointed out to me that the include-statement within my include_content-function only uses the variable scope of my function - meaning that any variables decleared outside my include_content is not passed to the include directive unless I make them global.
It was also suggested that I could pass an array of named keys=>$variables into my function and use extract() on them to make them available.
This is what I came up with:
- added $arr
public static function include_content($content_name,$arr){
if(file_exists(dir_pages_contents.$content_name.'.cont.php')){
if (is_array($arr)){extract($arr);}
include dir_pages_contents.$content_name.'.cont.php';
} else {
/* echo an error message */
}
}
Now I'm able to do this:
<?=site::include_content('test_doc',array('hello'=>$hello))?>
Allthoug I'm not happy with this solution, I now have the variables accessable within the included document - so I'm closer than I was an hour ago :)
Any easier way?
I am a newbie in codeigniter. I want to load jquery on every page. I know we have to use autoload.php. But inside it there is section where static contents like js or css can be included. How to do this? I created another index like this
$autoload['static'] = array(JS_LIBS_PATH.'/jq.js');
But obviously nothing happened. That constant has been defined in config,php. Using same constant I could put jq on a page, but what about autoload ?
I can think of several elaborate solutions but I really think that javascript is presentation-level and shouldn't be in your autoload, models, controllers, etc.
You can just use a master view file of some kind with your <head> and basic HTML requirements:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<script src="/path/to/jquery.js"></script> <!-- jQuery on every page -->
<!-- alternatively, loop through files in a custom config setting -->
<?php foreach (config_item('default_js') as $src): ?>
<script src="<?php echo $src; ?>"></script
<?php endforeach; ?>
<!-- ... and/or loop through files set in a controller -->
<?php foreach ($js_files as $src): ?>
<script src="<?php echo $src; ?>"></script
<?php endforeach; ?>
</head>
<body>
<header />
<?php echo $content; ?>
<footer />
</body>
</html>
Then just load views like this:
$data['js_files'] = array('draggable.js', 'widgets.js');
$content = $this->load->view('index', $data, TRUE);
$this->load->view('master', array('title' => 'Home', 'content' => $content));
Of course this is just one solution out of millions, but the idea of Codeigniter "autoloading" CSS and Javascript makes no sense, you have to "load" it yourself in one way or another - how you do it is completely up to you.
Here's what I've done for a while that works quite well.
Your asset files (javascripts, stylesheets, etc) go in your asset folders /assets/js, /assets/css and so forth.
In your config file, you define a default array of JavaScript files to load.
$config['default_asset_js'] = array('js/jquery.js', 'js/jquery-ui.js');
In your views, you have a section that runs through that array and outputs each of them.
foreach($this->config-item('default_asset_js') as $file_name)
{
echo '<script src="' . base_url('assets/' . $file_name) . '"></script>';
}
You can probably also get creative and add to that array in your controller, if you have specific pages that require additional files to be loaded.
In CI, Helpers, as the name suggests, help you with tasks. So we can load jquery automatically as a helper.
Put the jquery file in some place: /var/www/html/ci3/assets/js/jquery-2.1.4.min.js
Create a file /var/www/html/ci3/application/helpers/jquery_helper.php (you need use the _helper.php sufix).
Inside the jquery_helper.php file call the jquery-2.1.4.min.js file:
<script src="assets/js/jquery-2.1.4.min.js"></script>
Inside /var/www/html/ci3/application/config/autoload.php call the jquery_helper.php:
$autoload['helper'] = array('jquery');
Now the jquery will be load automatically and you can use it in all your views.
I want to include external js file in my php script. I am following zend framework.Right now I am adding js file in controller's init function like this.
public function init() {
$this->doUserAuthorisation();
parent::init();
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/front_cal/jquery-1.3.2.min.js');
$this->view->headLink()->setStylesheet($this->view->baseUrl().'/styles/front_cal/calendar.css');
}
problem what i am facing is, js file doesnot include.Is this the right way to include js file?
JavaScript (and images, CSS, flash movies, etc) belong to the view layer so configure them there.
For globally included files, add them to your layout, eg
<!-- layout.phtml -->
<head>
<?php echo $this->headScript()->prependFile(
$this->baseUrl('path/to/file.js')) ?>
<?php echo $this->headLink()->prependStylesheet(
$this->baseUrl('path/to/file.css')) ?>
<!-- snip -->
<?php echo $this->inlineScript()->prependFile(
'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js') ?>
</body>
Your view scripts can then add assets to the helpers which are echoed out in the layout. As the layout uses the prepend*() methods, the global files will be displayed first, eg
<?php // views/scripts/index/index.phtml
$this->inlineScript()->appendFile($this->baseUrl('path/to/script.js'));
In above solution the 'path/to/file.js' script will be included in the header twice. There is no need for echo before calling prependFile. Such behaviour can lead to duplicating javascript controls.
<!-- layout.phtml -->
<head>
<?php echo $this->headScript()->prependFile(
$this->baseUrl('path/to/file.js')) // this will echo out the whole prependFile queue
Excuse me if this is slightly newbie.
I have the main view located # app/views/index.php as:
<?php echo $head ?>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
</html>
The header_meta.php file located at (app/views):
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="icon" type="image/png" href="img/favicon.ico" />
<!--meta-->
the controller, named SpecialPage.php located at app/controllers/:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class SpecialPage extends CI_Controller {
function SpecialPage(){
parent::CI_Controller();
}
public function index()
{
$head = $this->load->view('header_meta', '', true);
$this->load->view('index', array('head' => $head));
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Error I'm getting on SpecialPage.php controller:
Call to undefined method CI_Controller::CI_Controller() on line 6
which is:
function SpecialPage(){
parent::CI_Controller();
}
Why is this still just showing a 404 error?????
I suppose it's this line:
<?php $this->load->view('header_meta.php'); ?>
The ".php" extension seems to be the culprit.
BTW, I do not recommend using PHP code in your views (except echoes and loops). Much better is to compose it in your controller:
$head = $this->load->view('header_meta', '', true);
$this->load->view('index', array('head' => $head));
Obviously, the "$t>l>v()" must be changed to "echo $head". Or, the way I prefer (using a template view):
$body = $this->load->view('index', '', true);
$this->load->view('template', array('body' => $body));
Your page should be named as:
specialPage.php instead of index.php under your controller folder.
Read here for more about controller naming conventions.
This is either expected to use like yourhost.com/index.php/specialPage or yourhost.com/specialPage (if your .htaccess rewrite is enabled).
Codeigniter tries to open a file depending upon what classname (via controller/model) you specify. Codenigniter has no idea why it should load index.php for your class files (unless you specify your classname as Index).
And I personally recommend not to use index.php for your files as it may confuse you and others, that this would be a self-loading file. Whereas, in codeigniter, the only self-loading file is index.php in the root folder of your codeigniter installation. And all other files are loaded through index.php (and files that it further includes) only.
The function SpecialPage() should be __construct() and parent::CI_Controller() should be parent::__construct();.
Good luck
Try to add this code to your controller maybe you miss this
function SpecialPage(){
parent::CI_Controller();
}
Note :
Make sure that your controller name is the same with the file name
And I have another question bro,How you call the page to load?
Gudluck!!
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 :)