I am trying to create a website with Symfony 2.0 and I am running into an issue where I see extend("...") ?> from <?php $this->extend("...") ?>.
The site is viewable at http://symfony.toxic-productions.com/install/web/poshpaws/hello.
The code for the controller:
<?php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
//return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
// render a PHP template instead
return $this->render('AcmeHelloBundle:Hello:index.html.php', array('name' => $name));
}
}
?>
The code for the frontend page (index.html.php)
<html>
<head>
<title>Poshpaws</title>
<?php
$view->extend('::base.html.php');
echo($head);
?>
</head>
<body>
<?php echo($body); ?>
<h1>This is just a page to say: Hello <?php echo $view->escape($name) ?>!</h1>
</body>
</html>
I just had the same problem. It turned out that I didn't have php templating engine enabled in my configuration file.
Make sure you check the first step from this site:
http://symfony.com/doc/current/cookbook/templating/PHP.html
Your .htaccess file either isn't working or isn't in the right place. Make sure it's in the web folder. If it is, then make sure your webhost allows you to override Apache settings. If it does, then run php app/console router:debug over SSH and make sure your route is properly defined. Also, you really should just set "web" as the root of your directory or you expose certain security risks needlessly.
On a side note...why PHP templates? To me, it doesn't enforce separation of concerns quite as well and they're generally harder to read (for me, anyway).
Related
I'm following the tutorial at Phalcon's page (https://docs.phalconphp.com/en/3.4/tutorial-base) and I got stuck at creating the /view/signup/index.phtml.
When I access the /signup/index.phtml page it only shows me the HTML tags, that is, "Sign up using this form". None of the $this->tag show. I copied the linkTo tag from the /views/index/index.phtml to the signup view but it didn't work as well, even though it's working perfectly in the index/index.phtml. Somehow no Tag is working at the /signup view.
Does anyone know why?
The codes
/app/controllers/SignupController.php
<?php
use Phalcon\Mvc\Controller;
class SignupController extends Controller
{
public function indexAction()
{
}
}
/app/views/signup/index.phtml
<h2>Sign up using this form</h2>
<?php echo $this->tag->form("signup/register"); ?>
<p>
<label for="name">Name</label>
<?php echo $this->tag->textField("name"); ?>
</p>
<p>
<label for="email">E-Mail</label>
<?php echo $this->tag->textField("email"); ?>
</p>
<p>
<?php echo $this->tag->submitButton("Register"); ?>
</p>
Views are not supposed to be accessed directly. Try to use Phalcon Developer Tools to set up your app skeleton or use the simple MVC example. Notice that you must have mod_rewrite enabled (if you are working in Apache). Then continue following the tutorial (create the Loader, DI with view, etc) and you will be surprised by the magic of Phalcon. Good luck!
Check your URL
Are you using /signup/index.phtml in your browsers address bar?
You should have something like localhost/tutorial/signup
You will be able to visually see templates by going to their path directly, but the php will not run.
Not all resources being included
If you are using the URL paths seen in the tutorial, check that all resources are being loaded into the SignupController correctly by doing the following:
var_dump($this->tag)
This should display something in regards to "form" or a whole bunch of text. If you see nothing the `Phalcon\Tag' class is not loading in correctly.
You probably may even have a typo somewhere. I would recommend copying and pasting the tutorial instead of typing, only while bug checking, to make sure there are no typos. You can use a tool like a diff checker to compare your files to their tutorial.
Online diff checker : https://www.diffchecker.com/
I would like to have the page on user/sidebar on the right in the template design.
Normally, I would include a php file. But I am using Kohana Framework so I have created a view and a controller for this sidebar, and exists on mysite.com/user/sidebar
Now how would i <?php include "/user/sidebar"; ?> correct? I get no such file og dir error for this. I tried full url, but allow_url_include=0
Just looking through the Kohana documentation...
It seems like you can include a "request" inside a view with the following command.
<?php echo Request::factory('user/sidebar')->execute() ?>
See this page for more info: http://kohanaframework.org/3.0/guide/kohana/mvc/views
AndrewR's comment is close. For Kohana 3.2, you'll want to load a view within a view and not a request within a view:
<?php echo View::factory('/user/sidebar'); ?>
or
<?php include Kohana::find_file('views', 'user/sidebar') ?>
Either is acceptable to do.
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 :)
Am sure the question is vague.
Let me try to explain.
Assume zend frame work - PHP - jquery combination.
I include jquery files in layout.phtml.
i include some files in controller.php.
some file in view.phtml
Atlast when i run and view the page . Is there any way or any tool to find which file is included through which file (layout controller or view) ??
In addition can some one explain which is the best way include js files and where . using zend framework in layout or controller or view
The only way to find where a public, static asset (JS, CSS, image, etc) is included is to trawl through the source code (using something that can "find in files" would save time).
In regards to how and where to include such assets... for global includes (common stylesheets, scripts, etc), include these in your layouts.
For specific page includes, place these in your views.
The best way to include a static asset is using the appropriate view helper. These are generally displayed in your layout file, for example
<?php echo $this->doctype() ?>
<html>
<head>
<?php
echo $this->headMeta()->prependHttpEquiv('Content-Type', 'text/html; charset=' . $this->getEncoding());
// I use "prepend" here so it comes before any page specific stylesheets
echo $this->headLink()->prependStylesheet($this->baseUrl('/css/common.css'));
echo $this->headScript();
?>
</head>
<body>
<!-- content -->
<?php echo $this->inlineScript() ?>
</body>
</html>
You can then add to these placeholders in your view scripts, for example
<?php
// index/index.phtml
$this->inlineScript()->appendFile('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js')
->appendFile($this->baseUrl('/js/my-jquery-script.js'));
To "include" a file means very different things in PHP (where it is analogous to copying and pasting source code from another file into the current file) and HTML/JavaScript (where you are referencing another file, which the browser must make a separate HTTP request to download). What do you consider "including"? Are image tags "including" the images? At least we can easily count those references by examining HTTP requests; from the client side, it's impossible to tell what include()s went into the source code behind the rendered output. Even naive source code searching couldn't tell you thanks to autoloading. As is, your question is not well enough defined to provide a clear answer.
Controversal answer:
You don't need that.
If you need that then it's something wrong with the way your designed your application.
Note: I've learned (trial and error) that 90% of things I don't know how to do and that seem to be impossible in ZF are a result of wrong application design.