Having the following class that is extended by other controllers
class Admin_Controller extends Base_Controller
{
static $admin_layout = 'admin.layouts.default';
public function __construct()
{
parent::__construct();
$role_object = User::find(Auth::user()->id)->roles()->get();
$role = $role_object[0]->attributes['name'];
such as:
class Admin_Draws_Controller extends Admin_Controller
{
public $restful = true;
public function __construct()
{
$this->layout = parent::$admin_layout;
parent::__construct();
}
public function get_index()
{
$view = View::make('admin.templates.draws');
$this->layout->content = $view;
}
}
How can I send the $role variable to admin.layouts.default so I can have it when ever the view is loaded?
The point of "global" $role variable is to avoid to have to call it in all of my View::make() like the following:
$view = View::make('admin.templates.articles',
array(
'fields' => $fields,
'data' => $results,
'links' => $links,
'role' => 'role here'. // I don't want to add this where ever I call the View::make
)
);
$this->layout->content = $view;
and just do an echo $role like, in my header.blade.php
I ended up doing the following, which works.
Controller
// Example of variable to set
$this->layout->body_class = 'user-register';
$view = View::make('modules.user.register', array(
'success' => true,
));
$this->layout->content = $view;
My default.layout.php view
<body class="<?php echo isset($body_class) ? $body_class : '' ;?>">
#include('partials.header')
{{ $content }}
#include('partials.footer')
The variable, can be easily used in any other context within the view.
Related
I am first day user of Twig, and I have some strange behaviour of engine.
I push some information to my view:
class MainController extends Controller {
public function actionIndex()
{
$template = self::$twig->loadTemplate('index.php');
$title = 'CRUD интерфейс';
$projects = MainList::showAll();
$workers = CompanyWorker::showAll();
$roles = Role::showAll();
$namesOfProjects = Project::showAll();
echo $template->render(array(
'title' => $title,
'projects' => $projects,
'workers' => $workers,
'roles' => $roles,
'namesOfProjects' => $namesOfProjects
));
}
}
In the end I have a good result but in the end of HTML file twig shows me my controller object. Why it's happened?
It's my Twig initialisation:
abstract class Controller {
public $loader;
static $twig;
function __construct()
{
$this->loader = new Twig_Loader_Filesystem('views');
//$twig = new Twig_Environment($loader, array('cache' => 'cache'));
self::$twig = new Twig_Environment($this->loader);
}
I'm were blind 8[. I really missed 'print_r' func.
Is it possible for a twig function to return a twig template?
For example
class ToTimeExtension extends \Twig_Extension {
public function getFunctions() {
return array(
'totime' => new \Twig_Function_Method($this, 'toTime')
);
}
public function toTime ($string) {
$time = strtotime($string);
return {"time.html.twig", $time};
//return a twig template and pass $time variable
}
}
time.html.twig
<h1>{{time}}</h1>
Usage
{{ totime('now') }}
You can access the Twig environment if you set the proper options. Then you can render another template inside the method.
class ToTimeExtension extends \Twig_Extension {
public function getFunctions() {
return array(
'totime' => new \Twig_Function_Method($this, 'toTime', ['needs_environment' => true,])
);
}
public function toTime (Twig_Environment $twig, $string) {
return $twig->render('time.html.twig', [ 'time' => strtotime($string),]);
}
}
GOT THIS ERROR Call to a member function parseCriteria() on null CAK
`
CONTROLLER
class ProductsController extends AppController {
public $components = array('Search.Prg');
public function index() {
// start a standard search
$this->Prg->commonProcess();
// process the URL parameters
$params = $this->Prg->parsedParams();
// generate the Paginator conditions
$conditions = $this->Product->parseCriteria($params);
// add the conditions for paging
$this->Paginator->settings['conditions'] = $conditions;
$this->set('products', $this->Paginator->paginate());
}
}
MODEL
class Product extends AppModel {
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'product' => array(
'type' => 'like',
'field' => 'name'
)
);
}
VIEW
echo $this->Form->create();
echo $this->Form->input('product');
echo $this->Form->end(__('Search'));'
class AppointmentsController extends AppController {
public function view($id = null) {
if (!$this->Appointment->exists($id)) {
throw new NotFoundException(__('Invalid appointment'));
}
$options = array('conditions' => array('Appointment.' . $this->Appointment->primaryKey => $id));
$this->set('appointment', $this->Appointment->find('first', $options));
$kk = $this->Appointment->find('first', array('fields' => 'status', 'conditions' => array('Appointment.id' => $id)));
$ss = reset($kk);
$stats = reset($ss);
}
}
I have set $stats via getting value from DB and i want to use in in another function in same controller
then I want to use like this
class AppointmentsController extends AppController {
function confirm(){
$stats = 'New';
}
}
Seeing that it's the same class, have you tried using $this->stats instead of a local variable?
You can call another function in your first function like
$this->confirm($status); // calling confirm function
function confirm($status)
{
//use status as you want
}
Or you can set status as global variable then this can you accessible in any function.
I think, you've to create function in the model that will return the value of the field by id.
<?php
// model/AppModel.php
public function getFieldById($field='id', $id){
return $this->field($field, array('id' => $id));
}
// in controller function you can access this like.
$status = $this->Appointment->getFieldById('status', $id); // $id will be appointment id.
?>
I have the following model:
class Person
{
public $name;
function __Construct( $name )
{
$this->name = $name;
}
}
I have the following controller:
class NavigationController extends Controller
{
public function indexAction()
{
$people = array(
new Person("James"),
new Person("Bob")
);
return $this->render('FrameworkBundle:Navigation:index.html.php', $people);
}
}
How do I get access to the model array in the view. Is there a way to access the model directly or do I have to assign a property like so:?
class NavigationController extends Controller
{
public function indexAction()
{
$people = array(
new Person("James"),
new Person("Bob")
);
return $this->render('FrameworkBundle:Navigation:index.html.php', array( "model" => $people ) );
}
}
View:
<?php
foreach( $model as $person )
{
echo $person->title;
}
?>
The problem with the above will be that it can be changed by a user to
return $this->render( 'FrameworkBundle:Navigation:index.html.php', array( "marybloomingpoppin" => $people ) );
With the example view you used, you already had the correct implementation:
class NavigationController extends Controller
{
public function indexAction()
{
$people = array(
new Person("James"),
new Person("Bob")
);
return $this->render('FrameworkBundle:Navigation:index.html.php', array( "model" => $people ) );
}
}
You mentioned the concern that somebody could change the assignment in the controller, but this is something you always have if somebody changes the name of a variable only in one place and not in all. So I don't think this is an issue.