I am newbie to the zend framework.
I want to know how to includes files in zend framework views.
I am using index.phtml to show the data in zend framework.
index.phtml contains some php pages like:
require_once ('/_incl/functions.php');
include('../functions.php');
Now I want to know where I put these files and call to index.phtml page.
What I can do to include these files, means where is the right place to keep all these files, and call from index.phtml page.
I totally understand how it feels to be a newbie.
No, you do not INCLUDE anything. If there is something you want to happen but have no option for it in zend, you make your own library and structure the class names accordingly. For example:
Zend_Form_Element is a class which is Zend / Form / Element.php
What you do is, make a class file and then, compile it.
To get started I would recommend going through this tutorial on tutsplus.
http://net.tutsplus.com/tutorials/php/zend-framework-from-scratch/
So, make a controller , make views, pass values from controller and then echo it in views.
Try to follow the rule-book rather writing codes in traditional method. :)
Hope you learn faster :)
I have simple way to include file in zend view page.
Just create one folder named 'include' inside public folder and store the file which you want to include.
After that write the following line in above the page where you want to include the file.
**include_once "./<include folder name>/<include file name>.php";**
Thanks......p2c
Related
I have a view helper that needs jquery, jqueryui, knockout and few other js files to work. Some pages already references all the js files needed by view helper, some pages don't.
Right now, I am using the url to figure out if a js file should be referenced or not inside the view helper.
Is there a better way to do this?
Thanks.
I'm not 100% sure I understand your question in the right way, but I'm 100% sure you need to manage your javascript dependencies in a better way.
I usually use require.js module loader to manage javascript files included into a template.
In order to use it, you need to download require.js file and place it somewhere in your project. I believe you would like to place it in ./public/js/vendors directory not to keep it alongside with your own code. Also, you need to create config file for it, where you decide what javascript modules are needed.
So, the concept is pretty clear: if you have javascript files, that are related to some part of your project, for example admin panel, you should create a config that will load those files. I usually have one config for admin panel part of my web application, and another one for user side of it.
This config with require.js itself are included into template like this:
<script data-main="./js/config-admin.js" src="./js/vendor/require.js"></script>
You need to keep in mind, that on every template you should have only one inclusion of javascript - the inclusion of require.js and it's config.
The config usually looks like this:
require.config({
baseUrl: '/public/js',
paths: {
'bootstrap': './vendor/bootstrap',
'jquery': './vendor/jquery'
},
shim: {
'bootstrap': ['jquery']
}
});
require(['./app'], function (app) {
console.log(app); //Here is smth that required module 'public/js/app.js' returns
//Do some coding here if you wish, for example to start js application.
});
If you need more examples of usage, you can visit my github profile, there are a couple of repositories where I use it (symfony and zend2 applications).
If you are not managing your assets via other means, the HeadScript view helper is designed to act as a collection of the scripts that should be rendered in the view.
Considering the helper depends on the JS files, placing these in the helper's factory class would be a logical place.
class MyViewHelperFactory
{
public function __invoke(ViewPluginManager $viewHelperManager, $name, $requestedName)
{
$headScriptHelper = $viewHelperManager->get('HeadScript');
$headScriptHelper->appendFile('/js/file.js');
return new MyViewHelper();
}
}
There are of course better ways of doing these things; personally I like Assetic.
I would like to add a new phtml file to my index folder in which I already have several views:
index
landing
And so on... I access them by using the following logic:
sitename.com/index/landing
or
sitename.com/index/index
How can I add the phtml file (my new view) to my index folder so that I'm able to see it when I enter in the browser:
sitename.com/index/mynewview
I'm quite new to the whole Zend Framework, and I'm not sure how the structure works exactly, so I'd like to find out more. Can you guys help me out with this, how am I supposed to do this?
Thanks heaps! :)
P.S. The views are in the following directory structure:
module/application/view/application/index/
and then here are all of the views, this is where I'd like to add my new view and access it from browser like this:
/index/testview
Edit:
When I add the testview.phtml to the index directory and put some test tags like this in it:
<h1> Testing new view page </h1>
It's not being rendered on the browser
Because this is an MVC framework, you're skipping a few steps. You're probably going to get a few harsh responses, but I'll try to fill in the holes for you very quickly.
Ignore the file folder structure for a minute.
This is a route:
/index/landing
Routes point to Actions inside of Controllers to work.
Assuming you have started with the skeleton, open up your module's module.config.php, you should see route config, e.g.:
https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#L29
You'll need to add a config entry for the routes you want to serve. It could be as simple as a Literal entry for /index/landing, or something more complex (Segments, Regex, etc.) that handle patterns for routing. Spend some time tinkering and learning here; routes are pretty critical to working with MVC.
When configuring the route, the assumption is that you have a Controller set up, and that this Controller has an Action (to which your route is pointed). That Action, is where you can connect template files (phtml,twig,etc.) to routes:
// dummy action that serves index/testview
public function fooAction(){
$vm = new Zend\View\Model\ViewModel();
$vm->setTemplate('index/testview');
return $vm;
}
That index/testview, will be in your module's view templates, not in your public folder.
I think that's a reasonable primer to get you on your way!
Take some time to learn:
http://zf2.readthedocs.io/en/latest/index.html#userguide
Maybe start here:
http://zf2.readthedocs.io/en/latest/in-depth-guide/understanding-routing.html
ZF2 (V3 is coming!) is a beautiful thing, it's worth it.
Good luck.
In one of my application I integrated a codeigniter template using HOOKS method... Its working pretty well ... The hooks/ template will call in Controllers Constructor ..
the 'default.php' is located in views folder ...
But I need 2 templates for my proj .. Can any one help me how to handle this ?
Please help
Look for the 'Themes' library... each theme has its own folder inside the views folder...and its called like this : 1st line is template 2nd line is the page
$this->themes->set_theme('theme');
$this->themes->set_template('template')
then in your controller its invoked using: $this->themes->view('view');
simple insert a {page_content} tag into your template where you want to insert the page code.
It may still be available on the CI wiki.
I have a website with many scripts written in "pure" PHP, i.e. no specific framework has been used to write the files. Furthermore, all the URLs are custom using .htaccess and specific PHP scripts.
For a smooth transition, I would like to start using CodeIgniter for new pages without disrupting access to the old pages, but all the documentation I've seen on CodeIgniter gives the impression that the whole website (perhaps with a few exceptions) needs to be based on the framework.
Would it be possible to use the framework for single pages here and there while leaving old URLs and code intact?
Short answer, yes.
You could access the CI framework from a subfolder, for instance, leaving the existing site untouched.
i.e
www.site.com/my_new_app/controller/method/
where my_new_app is the renamed application folder.
I'm going to go on the assumption that you already have a basic template system in place, and are able to render full pages with your existing site. Since Codeigniter is really just a framework, there's nothing to stop you from using vanilla php, like include, or additional libraries and classes. So, one thing you can do is dump your site into a sub directory in your views folder, then create a "master" controller which does nothing but load full html pages.
class Master extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// We're expecting something like "registration/how-to-apply" here
// Whatever your URL is. The .php extension is optional
$args = func_get_args();
$path = 'path_to_my_old_site/'.explode('/', $args);
$this->load->view($path);
}
}
// Then use this in config/routes.php
$route['(:any)'] = 'master/index/$1';
This will route all pages through the master controller. So, yoursite.com/pages/faq will load the file application/views/old_site/pages/faq.php. You can apply different routes as you see fit.
This way, you can take your time migrating to use Codeigniter conventions, one page at a time.
I have to add a feature to a quite big app using zend framework.
i have a view. in that view, i have a if and want to include an other .phtml on the same location.
so, at the moment i got something like
if (x = true)
require_once(the other file);
that works, but isnt in the meaning of zend. i've been told i should use view helpers, more specific, the partial. so, how do i include a phtml file with the partial? i dont get it.
Use the render() view helper like this:
<?=$this->render('the other.phtml')?>
All view variables available in the current .phtml script will be available in the other.phtml too.
If you want to render the other view script with specific view variables, use partial instead:
<?=$this->partial('the other.phtml', array(
'var'=>'value',
'var2'=>'value2',
...
))?>