 |
| Zend Framework 2 |
-:: Step 1 ::-
Create layout file
========================================================================
First Create a directory in your module,
Suppose we have User module, and I would like to add login page layout
then, create module/User/view/layout/login.phtml file into this directory
========================================================================
-:: Step 2 ::-
Call layout into your Controller Action
========================================================================
Simply Call the layout this way into
your controller
$this->layout('layout/login');
========================================================================
How To Use InlineScript for HTML Body Scripts
he HTML <script> element is used to either provide inline client-side scripting elements or link to a remote resource containing client-side scripting code.
The InlineScript helper allows you to manage both. It is derived from HeadScript, and any method of that helper is available;
however, use the inlineScript() method in place of headScript();
===============================================
Note::Use InlineScript for HTML Body Scripts
InlineScript, should be used when you wish to include scripts inline in the HTML body.
Placing scripts at the end of your document is a good practice for speeding up delivery of your page,
particularly when using 3rd party analytics scripts.
Some JS libraries need to be included in the HTML head; use HeadScript for those scripts.
===============================================
-:: Basic Usage ::-
Add to the layout script:
===============================================<body>
<!-- Content -->
<?php
echo $this->inlineScript()->prependFile($this->basePath('js/vendor/foundation.min.js'))
->prependFile($this->basePath('js/vendor/jquery.js'));
?>
</body>
===============================================
-:: Output ::-
===============================================
<body>
<!-- Content -->
<script type="text/javascript" src="/js/vendor/jquery.js"></script>
<script type="text/javascript" src="/js/vendor/foundation.min.js"></script>
</body>
===============================================
-:: Capturing Scripts ::-
Add in your view scripts:
===============================================
$this->inlineScript()->captureStart();
echo <<<JS
$('select').change(function(){
location.href = $(this).val();
});
JS;
$this->inlineScript()->captureEnd();
===============================================
-:: Output ::-
===============================================<body>
<!-- Content -->
<script type="text/javascript" src="/js/vendor/jquery.js"></script>
<script type="text/javascript" src="/js/vendor/foundation.min.js"></script>
<script type="text/javascript">
//<!--
$('select').change(function(){
location.href = $(this).val();
});
//-->
</script>
</body>
-:: More Details On ::-
===============================================| Source:: http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.inline-script.html |===============================================
FlashMessenger Documentation
Going by the documentation a simple use case for the flashMessenger would look like the following:
http://zf2.readthedocs.org/en/latest/modules/zend.mvc.plugins.html#the-flashmessenger
Step 1.
======================================================
Add Plugin on your Controller
======================================================
------------------
------------------
Use Zend\Mvc\Controller\Plugin\FlashMessenger <- Add this Plugin on your controller
------------------
------------------
Step 2.
In Controller, Your Code may be like this
public function commentAction()
{
// ... display Form
// ... validate the Form
if ($form->isValid()) {
// try-catch passing data to database
--------------------------------------------------------------------------
| $this->flashMessenger()->addMessage('Thank you for your comment!'); |
--------------------------------------------------------------------------
return $this->redirect()->toRoute('blog-details'); //id, blabla
}
}
public function detailsAction()
{
// Grab the Blog with given ID
// Grab all Comments for this blog
// Assign the view Variables
return array(
'blog' => $blog,
'comments' => $comments,
-------------------------------------------------------------
| 'flashMessages' => $this->flashMessenger()->getMessages() |
-------------------------------------------------------------
);
}
In Short, For Set the message Simply Call
$this->flashMessenger()->addMessage('Your Message Goes Here');
For Get the Message and set for view file, simply Call
$flashMessages = $this->flashMessenger()->getMessages();
Here $flashMessages is ready for to set the value for showing the message
As you can see by its name this is a class that you can call or access it in your view file.
So when you need a class in your view file like a function to calculate total or anything else.
You need to create “view helper” it’s difference with "action helper" because "Action helper" is intended to just call from action controller.
Step 1:
-----------------------------------------------------------------------
1. create a new directory and new file.
-----------------------------------------------------------------------
your_module_name/src/your_module_name/view/Helper/Foohelper.php
e.g Suppose you want to create a helper for User Module, file path should be like this
module/User/src/User/view/Helper/Foohelper.php
Step 2:
-----------------------------------------------------------------------
Add new file in it :
-----------------------------------------------------------------------
<?php
namespace User\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Foohelper extends AbstractHelper
{
public function __invoke()
{
return $this;
}
public function fooMethod( $test ){
echo $test;
}
public function barMethod(){
echo 'hai';
}
}
Step 3:
-----------------------------------------------------------------------
Edit your Module.php Of User Module
-----------------------------------------------------------------------
public function getViewHelperConfig()
{
return array(
'factories' => array(
'foo_helper' => function($sm) {
$helper = new View\Helper\Foohelper ;
return $helper;
}
)
);
}
Step 4:
-----------------------------------------------------------------------
How to use it in your view file :
-----------------------------------------------------------------------
<?php echo $this->foo_helper()->fooMethod('hello'); ?>
You don't have rights to access database. Execute
GRANT ALL PRIVILEGES ON *.* TO ''@localhost
and then
FLUSH PRIVILEGES
to get access.