phalcon - before executing view event? - php

I want to execute:
$this->view->setVar("menus",$menus);
before the view gets executed.
$menus is an array that can be added by different controllers.
Finally before executing the view i want to put the menus var in the view.

Pick one of the Dispatcher's Events that best suits your needs, then add a method in your controller with same name of the picked event. You can implement this method on your controller base class. For example, adding the $menus in all views for the indexAction:
class MenuControllerBase extends \Phalcon\Mvc\Controller
{
public function beforeExecuteRoute($dispatcher)
{
if($dispatcher->getActionName() == 'index') {
if(isset($this->menus)) {
$this->view->menus = $this->menus;
}
}
}
}

Related

Load different methods depending on which view is being browsed LARAVEL

Trying to load different views and methods depending on which view the user is browsing.
Views:
public function edit()
{
if("SOMETHING")return View::make('store_edit');
If("SOMETHING")return View::make('product_edit');
}
Methods:
public function destroy($id)
{
if(SOMETHING){
$store = Store::find($id);
$store->delete();
return redirect('/store');
}
if(SOMETHING){
$product = Product::find($id);
$product->delete();
return redirect('/product');
}
}
What can be used in the if() statements depending on which view is browsed in order to delete the right item and not having to rewrite the functions for each table.
There isn't a simple way to get information about which view was displayed in a previous request, and that's probably not what you want. You should create separate controllers/routes for both "products" and "store". Then you can do away with that view logic altogether.
To somewhat answer your question, you can access information about the current route with the Route facade.
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Read Laravel routing:
https://laravel.com/docs/5.6/routing#route-parameters
Route
Route::get('something/{param}', 'SomeController#edit');
Controller
...
public function edit($param) {
if ($param === $expectedParam) {...} else {...}
}
Also you can make $param optional:
Route
Route::get('something/{param?}', 'SomeController#edit');
Controller (don't forget to give a default value)
...
public function edit($param = null) {
if ($param === $expectedParam) {...} else {...}
}
Please do not use same controller for diferent models.
if has diferent view and diferent functions this HAVE to has diferent controllers
Again, Laravel is a beautiful framework, dont do this!

Share 2 data to all view in laravel 4.2

I want to share this data to all views with laravel 4.2
But, can I use array to pass this 2 data with View::share()?
class Sidebar extends BaseController {
public function __construct() {
$package_sidebar = TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get();
$artikel_sidebar = Artikel::orderBy('date','DESC')->take(4)->get();
View::share()
}
}
Put this in your routes.php or somewhere else more appropriate:
<?php
View::share('package_sidebar', TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get());
View::share('artikel_sidebar', Artikel::orderBy('date','DESC')->take(4)->get());
Then you'll be able to reference $package_sidebar and $artikel_sidebar in any view.

One Method Controller, Multiple View, Codeigniter

So, lets say I have a global view and specific view.
In global view, the url may look like this (http://example.com/index.php/controller/method/)
Where when it come to the specific page view, the url will turn like this:
(http://example.com/index.php/controller/method/1989-03-25)
The difference between the global view and the specific page view is, if in the global view it shows the information in general, but in the specific page view it shows based on the detail or the date.
Of course, not only have different view, but also they will have different function of models.
The point is, I just want to make the url keep in order (which it mean there is no change in the name of the controller method).
How to do this. Thanks in advance.
You create just one param into your function. And set the param value is null. like this
class YOUR_CLASS extends CI_controller {
public function method($params=null) //null is Empty value
{
if($params)
{
//load your modal and view with your param
}
else
{
//load your modal and view
}
}
}
This method supports the following type of URL's without any issue.
http://example.com/index.php/YOUR_CLASS/method/
http://example.com/index.php/YOUR_CLASS/method/1989-03-25
Hope this will help you....
This class used to wrap CI_controller, my_base_controller will override CI_controller methods for depends to your project needs
class my_base_controller extends CI_controller {
}
You can load models by known states and define it in my_base_controller class.
class my_controller extends my_base_controller{
public function method($params=null) //null is option value
{
}
}
Good luck!
You can add additional parameter in your method like:
class Your_controller extends CI_controller {
public function method($params = null)
{
// Your Code Here with the condition for processing of the $params variable
}
}
in which that $params can be something in your URL like:
http://example.com/controller/method/your-dynamic-params
So if the $params is null you will call the model the views the general and if the $params has a specific value you can call other model by using if or switch conditional statements. Hope this helps...
Update with Example
you can use the $params variable like this:
if ($params == "1991") {
$this->load->view('general.html', $params);
} elseif ($params == "1992") {
$this->load->view('year_1992.html', $params);
} else {
$this->load->view('other_years.html', $params)
}
in this way you can use the $params as a conditional variable to load different views.
or using switch:
switch($params) {
case '1991':
$this->load->view('general.html', $params);
break;
case '1992':
$this->load->view('year_1992.html', $params);
break;
default:
$this->load->view('other_years.html', $params)
}
Note: Use a helper method so you can avoid fat controllers because it will be hard to test your methods if you have a several lines of codes in a function.

Nesting controllers and views in Laravel

I have a controller in my Laravel project called ImageController. This is a pretty basic CRUD controller.
When I access /images/{id} through my ImageController#show action, I want to also display comments. However, I don't want to put the comment logic in my ImageController. For this logic, I have created an ImageCommentController.
I'm not really sure how to go about this, but I'm trying to do something of this sort:
class ImageController extends BaseController {
// methods ...
public function show($id)
{
$images = // get images ...
$this->layout->view = // images.show and imagescomment.index (using ImageCommentsController#index logic)
}
}
I'm sorry if this is vaguely phrased, let me know if it is and I'll try to make it more understandable.
Maybe a better solutions than using a Controller for displaying the comments is to use a class with a method renderComments() that basically does something like:
class Comments {
public static renderComments($commentType = 'images')
{
$comments = Comments::where('comment_type', '=', $commentType)->get();
return View::make('comments', $comments)->render();
}
}
Then for example inside your image view:
...
{{ Comments::renderComments() }}
...

How to set service manager globally from a controller in zf2?

In normal way i can able to define an object through out the application by defining a service factory in configuration file global.php
i can get the object in controller by just calling $this->getServiceLocator()->get('mycollection')
My code as follows:
In global.php
service_manager' => array(
'factories' => array(
'mycollection'=> function($sm){
$collectionAdapter = new Collection();
$collectionAdapter->addItem("testvalue",'test');
return $collectionAdapter;
}
By adding in global file i can able to retrieve
//`var_dump($this->getServiceLocator()->get('mycollection')->getItem("test"));// will return testvalue`
through out the application
But my issue is that i dont know how to accomplish set values to the service from a controller
My requirement is that i need to set the service in one controller and retrieve in another module
i tried the following code in my IndexController album module
$this->getServiceLocator()->get('mycollection')->addItem('testvalue28','test8');
and in another module student IndexController called
//var_dump($this->getServiceLocator()->get('mycollection')->getItem("test8"));//
How can i accomplish the same which i set in global.php in a controller . or more clearly i need to store the collection values to the entire application at one instance in all modules
Edited
1)The function addItem will be set only based on controller action
2)Is there any thing similar to ZEND_REGISTERY where i can set a value form a particular request and retrieve in another action
//An application controller is define where i need to set different key value pair
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function authenticateAction()
{
//----other code---------
var_dump($this->getServiceLocator()->get('mycollection')->addItem('userauthenticationobj','userkey'));
//$redirect=module=user controller action =index
return $this->redirect()->toRoute($redirect);
}
}
In user module index action i need to get the key value pair which is set inside application module in index action. i know this can be done using a session or db or cookies but i want to implement this using a singleton instance through out the application. i don't know to define the correct term in oops so defining the situation
//User controller
namespace User\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function authenticateAction()
{
//----other code---------
var_dump($this->getServiceLocator()->get('mycollection')->addItem('userkey'));
//$redirect=module=user controller action =index
return $this->redirect()->toRoute($redirect);
}
}
//IN GLOBAL.PHP i defined
return array(
'service_manager' => array(
'factories' => array(
'mycollection'=> function($sm){
$collectionAdapter = new Collection();
$collectionAdapter->addItem("testvalue",'test');
return $collectionAdapter;
}
),
),
);
//user defined collection reference: http://www.sitepoint.com/collection-classes-in-php/
namespace Application\Adapter;
class Collection
{
private $items = array();
public function addItem($obj, $key = null)
{
if ($key == null)
{
$this->items[] = $obj;
}
else {
if (isset($this->items[$key]))
{
throw new \Exception("Key $key already in use.");
}
else
{
$this->items[$key] = $obj;
}
}
}
public function getItem($key)
{
if (isset($this->items[$key]))
{
return $this->items[$key];
}
else
{
throw new \Exception("Invalid key $key.");
}
}
}
If you are in the same request (this is, the user is not following links, or the page is not being refreshed) the service manager will keep the Collection alive and what you are trying should work.
But if you are redirecting the user to another controller/action, or the user has followed a link, submited a form, or whatever that causes a new page to be loaded, all the values created in the previous page wont exists anymore. If you need to persist them, you should use sessions, cookies, database, etc.
If the values are not set during the action, i.e you dont need a controller to be loaded, but you need all the controllers to be able to add values to the collection on the application bootstrap, no matter what controller is actually loaded, you can add some code to every module, in Module.php onbootstrap function. for instance, in every module's Module.pho, you do:
public function onBootstrap(MvcEvent $e) {
$sm = $e->getApplication ()->getServiceManager ();
$collection = $sm->get('mycollection');
$collection->addItem('testvalue_N','test_N');
}
and then, in every controller/action that is executed, you will have the collection with all the items added by all the modules

Categories