Including a uri "/user/sidebar" page (Kohana Framework) in PHP - php

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.

Related

php file showing output from a different file

I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.
Home.php - The first file (outputs 'why am i executing' at the end)
<?php include 'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php - The second file
<?php
include 'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>
Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.
I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.
Thank you.
Flip all these to require_once.
<?php require_once'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
require_once'db.inc';
require_once'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php
<?php
require_once'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
require_once'db.inc';
require_once'dao.inc';
?>
include breaks when you try to include the same file twice.
Also look up composer and into using a php framework so you dont have to worry about any of this!

Joomla Component: include/require php in View

this is my first time making a component for Joomla 3.
I'm trying to convert a php website to a component.
Almost every view of my php website is built this way:
Header
Sidebar
Navigation bar
Actual content
Footer
Scripts
So basically it looks like this:
A view from php website
<?php require_once('includes/header.php'); ?>
<body>
<div">
<?php require_once('includes/sidebar.php') ?>
<div>
<?php require_once('includes/navbar.php') ?>
</div>
<div>
<!-- Content or something -->
</div>
<?php require_once('includes/footer.php') ?>
</div>
<?php require_once('includes/scripts.php') ?>
<script>
</script>
</body>
</html>
But in Joomla this is not possible. If I try to include or require a php file like above it just doesn't work.
View in component looks like this:
com_component
../views
../tmpl
default.php
view.html.php
../includes
header.php
footer.php
sidebar.php
scripts.php
navbar.php
Default.php is supposed to show a dashboard on the frontend.
This is what I'm trying to do in default.php:
Default.php
<?php include_once('../../includes/header.php') ?>
<body>
<?php include_once('../../includes/sidebar.php') ?>
<?php include_once('../../includes/navbar.php') ?>
<!-- Some content-->
<?php include_once('../../includes/footer.php') ?>
<!-- etc -->
What I've done
I've found some JDocument and JHTML functions which can add stylesheets and javascript to the template. But that is not what I'm looking for.
I used Joomla's addCustomTag function but it only shows a commented php line in the template.
Tried to make a String of it and passed it through a variable.
Questions
Is it possible to include php files in a template (default.php)?
If not, is there any other way to do it?
If yes, is that good practice in Joomla?
Thanks
To include files in the same directory as the given file, you can use:
include __DIR__ . '/../includes/header.php'
If you don't use DIR the current path is related to the main page (index.php in the site root) and not to the layout (default.php).
A more Joomla compatible solution is to put all your "sub-layouts" in the same folder with a conventional naming like this:
com_component/
views/
myview/
view.html.php
tmpl/
default.php
default_header.php
default_footer.php
....
Then you can use in your main layout the following code:
echo $this->loadTemplate('header');
...
echo $this->loadTemplate('footer');
You don't need a component to do any of this. I mean you can
do this but it should only be as an intermediary step since Joomla is designed to make all this easier by having APIs to include modules which is basically what all your pieces are.
You'll notice that in the joomla template file there are places where module positions are loaded. These are things like menus, footers, sidebars etc.
Depending on what they are you should really make each of these subsections a module. In some cases you can probably use existing modules already in Joomla or make a "custom html" module if you are just loading some html. In other cases makin a joomla module is really easy, just a few files, and you can put your php code there, really to start you can put the whole thing in the tmpl/default.php for the module or you can split it between the helper and the layout.
You'll be much happier in the long run if you take advantage of using a CMS rather than fighting it.
Components are only for the "body" area, what you need to do is make a template and then the modules.

Generating PDFs from template files using wkhtmltopdf

I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>

Zend - How to include file in view like masterpae asp.net?

I have structure folder like this:
http://nx9.upanh.com/b4.s37.d4/3fd103509d1ac25ece02096b5e25a5f0_55810779.capture.png
(I really sorry, I'm not enough reputation)
I want to include 2 files are header.phtml and footer.phtml from public/shared/ folder to view of default module, I use code below:
<?php echo $this->partial('public/shared/header.phtml'); ?>
<h3>This is index.phtml</h3>
<?php echo $this->partial('public/shared/footer.phtml'); ?>
But I get an error HTTP 500 (Internal Server Error). If I'm only using html code, there is no problem happen.
<h3>This is index.phtml</h3>
I can do like this in CodeIgniter Framework but I don't know how to do this in Zend Framework. Please help me! Thank so much.
Are you using ZF1 or 2 ? In ZF1, you will want to place the header and footer into the layout.phtml file located in application/layouts/scripts/layout.phtml.
The actual header.phtml and footer.phtml should be placed into the application/view/scripts folder, then you can do something like:
LAYOUT.PHTML:
<?php echo $this->partial('header.phtml') ?>
// this bit will render the action content
<?php print $this->layout()->content ?>
<?php echo $this->partial('footer.phtml') ?>

How do you include PHP files in the view file of a Zend/MVC?

Maybe I dont understand the MVC convention well enough, but I'm trying to include a file to the index.phtml view for the main Index Controller, and it keeps giving me an Application Error. I have no idea what this error is or why its not working. But I'm using a standard include_once(...) in the view.
Is this even allowed?
A view in Zend is still just a php file. If you are getting errors in a view using include_once(), they are probably because the file you want can't be found in your include path. Trying dumping get_include_path() into the view and you will see what directories PHP is searching to find your included file.
As an alternative to include_once, you could use
<? echo $this->render('{module}/{action}.phtml') ?>
to pull in the file.
There are partial views for such purpose
http://framework.zend.com/manual/en/zend.view.helpers.html (ctrl+f Partial Helper)
The view is only the HTML that will be rendered. It's the very last thing that is processed. The controller is called first, then it calls whatever models are needed within. After passing all the data to the view, the view's HTML is rendered.
In short: whatever you include in the view, the controller isn't aware of it. You need to run your PHP includes earlier in the code. If you do it in the controller, it should work OK, I suppose (not tested, so I don't guarantee anything).
You can use Zend View Helpers for this purpose
last time this works fine for me. You can try this:
<?php echo $this->partial('common/left_menu.phtml'); ?>

Categories