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!!
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?
The hardest part I'm having with learning Codeigniter is the URI and URL 'theory' if you will.
I followed the original tutorial concerning static pages, and now the code seems to mess EVERY single link up except the main nav bar, requiring me to constantly add routing parameters. I figure I must be doing something wrong.
In my controller, I currently have this code, based off the tutorial:
public function view($page = 'home') {}
My folder structure is:
+ applications
+ views
- welcome.php
+ main
+ css
+ js
- home.php
- about.php
- etc.php
I should point out that the welcome.php page is for the login page. On that page, a link will direct you to home.php (main/home/)
My routing code looks like this:
$route['default_controller'] = 'welcome';
$route['main/(:any)'] = 'main/view/$1';
$route['main/home/home'] = 'main/view/$1';
$route['404_override'] = '';
As you can see, I already had to put a bandaid on it with the <code>$route['main/home/home'] = 'main/view/$1';</code> portion, due to the fact that clicking on "home" while already on the home page would result in linking to main/home/home/ displaying my nav bar, and creating a brand new set of missing links labeled, main/home/about/
In short, I am now trying to reference a .js file and a .css file, but even though the links correctly point to /main/css/style.css it does not recognize it.
Here is my view code for the header (where i load my .css and .js)
<html>
<head>
<title><?php echo $title ?> - TownBuilder - Prototype</title>
<link href="css/structure.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<header>
<ul>
<li> <?php echo $username ?></li>
<li>Account</li>
</ul>
</header>
Any advice on how to set up routing so that it works correctly?
IMHO they need to remove the route's piece from the codeigniter tutorial because it's not necessary, and it screws everyone up.
Here's the gist of how things work. First comment out all of your routes.
Controller:
Class FirstController extends CI_Controller {
public function home() {
// do stuff
$this->load->view('home');
}
}
Make sure you have a view called home.php in your view folder.
Then the url should be <base_url>/controller/method, so in this case it would be <base_url>/FirstController/home
I think you should add the css/js file on the root directory(outsite of the application folder crete a folder called css) as CI has .htaccess files that deny all access to the application folder. You can only include those files from within your scripts.
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.
I am making a simple application and I am familiar with CodeIgniter 1.7.3, which I didn't have any problems putting sub folders into the application/views folder.
My problem is specifically when I try and load the view, I get the standard CI error message of An Error Was Encountered Unable to load the requested file: usercontentpages/testview.php
However if I move the view file out of the sub folder and place it in the application/views folder the load view command works fine?
I can't find any documentation in the user guide on why this is so?
Here is my Controller:
<?php
class Pages extends CI_Controller
{
//Controller Constructor
public function __construct()
{
parent::__construct();
}
//Home Page (Default)
public function index()
{
//Loading a Partial View
$mContent = $this->load->view('home', '', TRUE); //this works
//$mContent = $this->load->view('subfolder/home', '', TRUE); //this doesn't work
$data = array(
'mainContent' => $mContent,
);
//Passing the partial view to the master page
$this->load->view('usermasterpage', $data);
}
}
?>
Here is my Master Page:
<?php
//Doctype
echo doctype('html');
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body onload="OnLoad()">
<div id="wrap">
<!-- Main Content Div -->
<div id="mainContent">
<?php
/* Every page will have a main content area */
if(isset($mainContent))
{
echo $mainContent;
}
?>
</div>
</div>
</body>
And Finally here is the page snippet I'm trying to load:
<p>Hi there</p>
CodeIgniter will successfully use nested views: I use nested views all the time and I know others who do as well, and it works out of the box. Show some code so we can see what you're doing wrong.
The CodeIgniter documentation says precisely that you can.
Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need to include the folder name loading the view. Example:
$this->load->view('folder_name/file_name');
http://codeigniter.com/user_guide/general/views.html
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 :)