Easy passing data to views

Submitted by iraitc - 9 years ago

This Trait helps you to pass "variables" anywhere in controllers to "view" . you can use "$this->view->..." in controller to pass your variables , also you can use "$this->view()" same as "view()" helper with same functionality to render the view . ( this package is made by larabook.ir)

<?php
 /*
 
usage in controllers : 

class HomeController extends Controller
{
use EasyViewLoader;

public function home()
    {

        $this->view->with(['var1'=>'will be deleted']);

        $this->view->with('name', ['hamed', 'mehrsa']);
        $this->view->withFamily('pakdaman');
        $this->view->gender = 'Male';

        $site = 'www.larabook.ir';

        if (isset($this->view->var1))
            unset($this->view->var1);

        // get all variables will pass to view
        print_r($this->view->getData()); 
        
        return $this->view('homepage', compact('site'))->withErrors('some errors')->withSuccess('some ...'); '
        // instade of using view() helper
    }
}
*/


 
trait EasyViewLoader
{

    public function __get($prop)
    {
        if ($prop == 'view' && empty($this->view))
           return  $this->view = new  Factory(view());
        return $this->{$prop};
    }


    public function __call($method, $parameters)
    {
        if (strtolower($method) == 'view') {
            return call_user_func_array(array($this->view, 'render'), $parameters);
        }
        parent::__call($method, $parameters);
    }
}

/*
   This Class helps you to pass  variables from whatever in controllers to view
   Author : Hamed Pakdaman
   Website: www.larabook.ir
*/
class ViewHelperFactory{
    public $factory;
    public $data=[];


    function __construct($factory)
    {
        $this->factory = $factory;
    }

    /**
     * Add a piece of data to the view.
     *
     * @param  string|array $key
     * @param  mixed $value
     * @return $this
     */
    public function with($key, $value = null)
    {
        if (is_array($key)) {
            $this->data = array_merge($this->data, $key);
        } else {
            $this->data[$key] = $value;
        }

        return $this;
    }

    /**
     * Add validation errors to the view.
     *
     * @param  \Illuminate\Contracts\Support\MessageProvider|array $provider
     * @return $this
     */
    public function withErrors($provider)
    {
        if ($provider instanceof MessageProvider) {
            $this->with('errors', $provider->getMessageBag());
        } else {
            $this->with('errors', new MessageBag((array)$provider));
        }

        return $this;
    }


    /**
     * Dynamically bind parameters to the view.
     *
     * @param  string $method
     * @param  array $parameters
     * @return \Illuminate\View\View
     *
     * @throws \BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        if (starts_with($method, 'with')) {
            return $this->with(snake_case(substr($method, 4)), $parameters[0]);
        }

        throw new BadMethodCallException("Method [$method] does not exist on view.");
    }

    /**
     * Get the evaluated view contents for the given view.
     *
     * @param  string $view
     * @param  array $mergeData
     * @return \Illuminate\View\View
     */
    public function render($view = null, $data = array(), $mergeData = array())
    {
        if (func_num_args() === 0) {
            return $factory;
        }

        return $this->factory->make($view, $this->data, array_merge($data,$mergeData));
    }


    /**
     * Get the array of view data.
     *
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }


    /**
     * Get a piece of data from the view.
     *
     * @param  string $key
     * @return mixed
     */
    public function &__get($key)
    {
        return $this->data[$key];
    }

    /**
     * Set a piece of data on the view.
     *
     * @param  string $key
     * @param  mixed $value
     * @return void
     */
    public function __set($key, $value)
    {
        $this->with($key, $value);
    }

    /**
     * Check if a piece of data is bound to the view.
     *
     * @param  string $key
     * @return bool
     */
    public function __isset($key)
    {
        return isset($this->data[$key]);
    }

    /**
     * Remove a piece of bound data from the view.
     *
     * @param  string $key
     * @return bool
     */
    public function __unset($key)
    {
        unset($this->data[$key]);
    }
}