Symfony Templates within Templates - php

Sorry if this seems incredibly simple. I am only new to Symfony.
I just want to have a template/view which "contains" other views. So, for the following example, imagine on my /dashboard/ it will show both "statistics" and "inbox". I want the code for each of these to be within separate actions/methods.
<?php
class dashboardActions extends sfActions
{
public function executeIndex(sfWebRequest $request) {
// Load statisticsSuccess
// Load inboxSuccess
// Render them both "within" index template
}
public function executeStatistics(sfWebRequest $request) {
// Render statisticsSuccess
}
public function executeInbox(sfWebRequest $request) {
// Render inboxSuccess
}
}
Thanks for any assistance you can provide!

Make "statistics" and "inbox" components. Be sure to reference the documentation of the version of Symfony you're using.

Related

Using the default public function index() in creating custom controller in CodeIgniter

I am new to CodeIgniter framework and just thought to ask this when I am studying the framework. I just want to ask, is including the function public function index(){....} before adding other functions after that? Or you can not necessary add the function and just load a view? For example, if I don't create the public function index(){...} and just add a function, let's say public function test_index(){..} and inside it is load the view..
I see the whole point now. I can add a function other than the public function index(){..} and just call or load the view inside it. For example:
public function test_index(){
$this->load->view('home_view);
}
and just view in the browser together with the class and the function..
You can add other functions as you wish with or without index

How to add new view to Phalcon app?

I'm completely confused on how Phalcon PHP renders its views. I want to create a new page called "manager".
From my understanding by creating a controller I can link it to a view. I create a controller called ManagerController.php and then added a view in views/manager/index.volt.
I added a bit of text the volt file to check if it works. When I go to /manager/ nothing shows up.
Am I doing this correct or do I have to assign a view somewhere?
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
}
The initialize function on a controller is an event ran after constructing the controller
In order to display view for that controller it is necessary to at least setup an index action
In your you are interested in rendering a route of /manager/ , this will correspond to indexAction
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
public function indexAction()
{
// This will now render the view file located inside of
// /views/manager/index.volt
// It is recommended to follow the automatic rendering scheme
// but in case you wanted to render a different view, you can use:
$this->view->pick('manager/index');
// http://docs.phalconphp.com/en/latest/reference/views.html#picking-views
}
// If however, you are looking to render the route /manager/new/
// you will create a corresponding action on the controller with RouteNameAction:
public function newAction()
{
//Renders the route /manager/new
//Automatically picks the view /views/manager/new.volt
}
}

Make things available in any view file Laravel

I have a question about making some elements available in any view file
Lets say im building a webshop and on every page i will be having my sidebar, shoppingcart, user greeting in the top.
How can i make these things available in all my view files?
I could make a class, lets say class Frontend
I could do something like this:
class Frontend {
static $me;
public function get(){
if(!self::$me){
self::$me = new self();
}
return self::$me;
}
private function getShoppingCart(){
// do things
}
public function getData(){
return array(
'Username' => User::find(1)->UserName,
'Cart' => $this->getShoppingCart()
);
}
}
Now in my controller i could pass this Frontend object into the view
View::make('file.view')->with(array('data' => Frontend::get()->getData()));
But this way i will end up with a god class containing way too much stuff and in every controller method i would have to pass these data, which is not relevant to the controller method
Is there a way in Laravel that makes specific data available across all view files?
Thanks!
Use share:
View::share('name', 'Steve');
as per http://laravel.com/docs/responses#views
To keep everything clean, every part of the page should be its own *.blade.php file which would be put together using a master template of sorts.
master.blade.php
#yield('includes.sidebar')
#yield('users.greeting')
#yield('store.shoppingcart')
Then you can use view composers so that each time these views are loaded, the data you want is injected into them. I would probably either create a new file which would get autoloaded, or if you have service providers for the separate portions of your app that these views would use, it would also go great in there.
View::composer('users.greeting', function($view)
{
$view->with('user', Auth::user());
});
In this case, it would make the user model available inside your view. This makes it very easy to manage which data gets injected into your views.
You are close with your 'god class' idea.
Setting a $data variable in a basecontroller has helped me with similar issues
class BaseController extends Controller {
protected $data;
public function __construct() {
$this->data['Username'] = User::find(1)->UserName
$this->data['Cart'] = $this->getShoppingCart()
}
}
class Frontend extends BaseController {
function someMethod(){
View::make('file.view', $this->data)
}
}

Is there a way to have multiple controllers for a symfony 1.4 module?

I have a situation where I have a base controller (a base actions.php file) in Symfony 1.4. I want to create another controller, for the same module, that extends that base controller.
I need to extend that base controller because I want to customize the behavior of certain visitors, that are identified based on an ID in the URL.
Any hints?
Another controller class for the same module, I think it's impossible in symfony.
I guess the easiest solution for you is to create another method in the same class, and then invoque it from the base one.
By Example: actions.class.php:
public function executeBaseAction(sfWebRequest $request) {
.. if($user....) then return $this->executeCustomAction($request);
}
public function executeCustomAction(sfWebRequest $request) {
// $this->setTemplate('anotherTemplate?');
}
Actually you can add another controller class for the same module.
You could include several files in your action directory in this way:
In action1Action.class.php
class action1Action extends sfAction
{
public function execute($request) {
//Your code here
}
}
This will use template action1Success.php
In action2Action.class.php
class action2Action extends sfAction
{
public function execute($request) {
//Your code here
}
}
This will use template action2Success.php

CodeIgniter adding page doesn't work

I have taken over a project written in CodeIgniter, which I have never used before. I have added a new file to views/pages called features.php, and have read on the internet that to make it accessible, I need to create a function in the controller file that will render the page.
I have tried the following:
public function features()
{
$this->render('template', 'pages/features');
}
However, when I try to open features.php, it gives me 404. How can I fix that?
Update 1 - Class
Here is the controller's class code:
class Pages extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('setting_model', 'setting');
$this->load->model('order_model', 'order');
$this->load->model('page_model', 'page');
$this->load->library('form_validation');
$this->load->helper(array('inflector', 'string'));
}
public function index()
{
$settings = $this->setting->get_settings();
$data['document_price'] = $settings->document_price;
$this->render('template', 'pages/index', $data);
}
//This works fine
public function about_us()
{
$this->render('template', 'pages/about_us');
}
//Here is the problem, although it follows the same pattern as about_us()
public function features()
{
$this->render('template', 'pages/features');
}
}
As you are using $this->render I guess you are using the template library. I think you should be using:
public function features()
{
$this->template->set_template('template');
$this->template->write_view('pages/features');
$this->template->render();
}
The php files contained in /views are not directly accessible by typing in some URL. CodeIgniter is an MVC framework. That means that your URLs are mapped to your controllers and the controllers call the views.
What is the name of the class that this function is encapsulated in? Please post the entire class and not just the features() function and we can help you out. If you're working locally, the default mapping to call controllers is: http://localhost/appname/controller/function/param1/param2/etc.
The $this->render() function is not vanilla CodeIgniter syntax, you either inherited a project that is using a templating library, or, there is a sibling render() function inside the controller class.
Check your config/routes.php file as well and consider posting it.
If you want to diagnose the issue, try pinpointing by removing the call to $this->render() and instead using CodeIgniter's native $this->load->view('pages/features') function. If this works, we can be sure it's the library or render() call.

Categories