Ask your Symfony questions! Pay money and get answers fast! (more info)

Why would a call to $context() in sfPHPView crash the page? Symfony

  • SOLVED

Hacking sfPHPView.php, the core class. Tried this in the method render:

$context()->getConfiguration()->getApplication();


Just calling the line seems to crash the page - I get a blank white screen. Any reason for this?

Earlier, I stupidly called sfContext::createInstance(), which was blanking the page since it recreated the instance. Then I changed to:

sfContext::getInstance()

This works better.

But why would getConfiguration or getApplication give me a white screen?


[UPDATE]

I apologize, I wrote this question poorly, and without enough information.

This is what I have right now in sfPHPView.class.php:

$context = sfContext::getInstance();

echo get_class($context);

$request = $context->getRequest();
$context()->getConfiguration()->getApplication();
// $module = $request->getParameter('module');
// $action = $request->getParameter('action');

// $customTemplateName = $application . '_' . $module . '_' . $action;
$path = '/home/' . getenv('TMA_USER_ID') . '/templates/';

echo $path.$customTemplateName;




The full function right now looks like this:




/**
* Renders the presentation.
*
* @return string A string representing the rendered presentation
*/
public function render()
{
$content = null;
if (sfConfig::get('sf_cache'))
{
$viewCache = $this->context->getViewCacheManager();
$uri = $this->context->getRouting()->getCurrentInternalUri();

if (!is_null($uri))
{
list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
if (!is_null($content))
{
$this->setDecoratorTemplate($decoratorTemplate);
}
}
}

// render template if no cache
if (is_null($content))
{
// execute pre-render check
$this->preRenderCheck();

$this->attributeHolder->set('sf_type', 'action');

// render template file


$context = sfContext::getInstance();

echo get_class($context);

$request = $context->getRequest();
$context()->getConfiguration()->getApplication();
// $module = $request->getParameter('module');
// $action = $request->getParameter('action');

// $customTemplateName = $application . '_' . $module . '_' . $action;
$path = '/home/' . getenv('TMA_USER_ID') . '/templates/';

echo $path.$customTemplateName;



if (file_exists($path.$customTemplateName)) {
echo $path.$customTemplateName;
}

echo $this->getDirectory().'/'.$this->getTemplate() ;
$content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());

if (sfConfig::get('sf_cache') && !is_null($uri))
{
$content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecorato$
}
}

// now render decorator template, if one exists
if ($this->isDecorator())
{
$content = $this->decorate($content);
}

return $content;
}
}




The weird thing is, I get a blank white screen, and yet no error is showing up in the error log.

This is Symfony 1.2.5

Okay, got this error:


[Thu Nov 11 11:56:26 2010] [error] [client 209.131.116.82] PHP Fatal error: Function name must be a string in /home/tma/core/cache/contest/prod/config/config_core_compile.yml.php on line 3868


And yes, I have cleared the cache.

Answers (4)

2010-11-09

Jimish Gamit answers:

try following one
sfContext::getInstance()->getConfiguration()->getApplication();

2010-11-09

Alex Zgorzhelsky answers:

there are 2 possible ways to reach context in sfView
1. $sf_context
2. sfContext::getInstance()

The second applies to all layers of symfony application

2010-11-10

Joshua Estes answers:

$context() isn't valid php. You need to use $contect->method()


Joshua Estes comments:

in a controller you can use $this->getContext()->getInstance()->method()

in a view (template file) you can use $sf_context->method()

in other places you will need to use sfContext::getInstance()->method()

If you find that you use sfContext::getInstance() in a lot of places, way way to get around this is to hook into the context.load_factories event. Once all the factories are loaded, this event is notified. You will then be able to use your own code to load an instance of sfContext into whatever classes you want.


Joshua Estes comments:

I'm not too familiar with symfony 1.2.5 so I'm not sure if you are trying to call a command that has been deprecated or something else. Regardless there is some type of error getting thrown some place.

Does it spit out any of the echo statements you have? Have you changed anything else in the symfony core files?

One thing I'm curious about is

$context = sfContext::getInstance();
echo get_class($context);
$request = $context->getRequest();
$context()->getConfiguration()->getApplication();


The last line, where you have <strong>$context()->getConfiguration()->getApplication();</strong> I'm not sure if that is a typo or what, but $context() isn't valid PHP. It would need to be $context->getConfigureation()->getApplication();

I'm also curious to why you are calling this? You aren't saving the output and your not giving it any parameters.


Joshua Estes comments:

Yeah, that error message you got is being caused by $context() it is saying that $context isn't a string. For example:


$context = 'className';
$var = new $context();


Joshua Estes comments:

Sorry for posting so many replies, but here's another way to use the same code.


function Test()
{
echo 'Blah';
}

$var = 'Test';
$var(); // outputs Blah

2010-11-08

Nate Flink answers:

I'm not sure but I think the short cut variable is supposed to be referenced in anything after the action code has executed, $sf_context

sfPHPView is a class associated with the view, and probably gets executed after the action.

http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer

It might depend on which version of symfony it is, whether $sf_context is assigned in the view or not.

Regardless, my original reading of the question, I misunderstood. Generally speaking: sfContext::getInstance() should work anywhere.

The only exception to this I have found is sometimes I think sfContext::getInstance()->getConfiguration() is not defined yet sometimes if it's a task, because tasks don't use the project configuration instance. I think I recall encountering some weirdness to that effect.


Lawrence Krubner comments:

Nate, I think $sf_action is only available in the templates.