global variables not working in symfony2 - php

The simple question is how do namespaces affect global variables?read the rest for more info
I have a working website in php.All the link addresses are made using get queries.(e.g:example.com/?page=contact instead of example.com/contact.php).i have overriden the super global variables in my symfony controller and configured the route.so that works.
my problem is with the global variables.there is a main index.php file which i have included in my controller (indexController) and that has all the code for the website.but the global variables don't work anymore(they've disappeared).i figured that was because of controller namespace but in the php documents it states that namespaces don't affect variables.so what could the problem be?the index.php would generate the expected results if run seperately but here i get undeclared variables)
this would be my code:index.php creates $html with HTML data
namespace example\exampleBundle\Controller
class indexController{
function indexAction(Request $request){
$request->overrideGlobals();
include "/external/index.php";
$response = new Response();
$response->setContent($html);
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type','text/html');
return $response;
}
}
------------------------
symfonytest:
path: /
defaults: {_controller: exampleexampleBundle:index:index}

I have found out the reason.since the library is included inside a function(indexaction) it is not declared in the global scope.I must use the global modifier inside the included index.php. I will report the results of that.

Related

can't bind href with php function

I'm working on a new website. This website will be a one pager. All my files I already load in through PHP into the main folder. But now I want to edit them and update them through WYSIWYG.
The UPDATE and SELECT are already working. I tested it on a page who stood on its own. All the one-pager files are stored in a folder and within the folder is also the file which loads all the files into it. I call them through a href which ends up giving the file an # in the address bar. There lies the problem. I can't access the #file with the function I wrote because that only can access files without starting a #. Is it possible to access it through my function?
I give the code if the question is too unclear because it's a bit of mess to implement all the files I use for this purpose.
Short recap: Can't access #domain with a PHP function. Is it even possible to access it.
<?php
class Home extends Controller {
protected function frontpage() {
$viewmodel = new HomeModel();
$this->returnView($viewmodel->frontpage(), true);
}
}
?>
this is the returnView from Controller:
protected function returnView($viewmodel, $fullview){
$view = 'views/'. get_class($this). '/' . $this->action. '.php';
if($fullview){
require('views/main.php');
} else {
require($view);
}
}
I'm new with overflow so I couldn't get the function into right place but with protected function frontpage I should access the file frontpage.php. Well it does do that but that's not the right directory because it's only visible throug a href which means the function should have been: protected function #frontpage which isn't possible.
This is actually not possible, as the anchor (#) isn't sent to the server and is handled by the browser itself.
The only possibility is to include some JavaScript magic to your project.

Joomla 3! Module Parameters

I have a Joomla website where I have a custom module with mod_myModuleName.php and mod_myModuleName.xml files and a folder where there are several PHP scripts that add special functionality to my module. There is a config.php file in the folder that holds an associative array with variables and their values hard-coded. The module works just fine.
What I want though is to provide administrator area for the values of the variables in the array, so that I can put values in administrator panel and get their values in config.php. In my mod_myModuleName.php I use <?php echo $params->get('param')?> and it works like a charm.
But when I try to use the same technique in the config.php it breaks my code. I tried to get the values in mod_myModuleName.php and then include it in config.php and use the variables but it does not work either. I have not got so much experience in php and cannot understand what can be the reason.
It sometimes gives me an error of something non object and I guess it must be something connected with object oriented php, am I right? And if so is there a way to overcome this without object orientation or how can I solve my problem?
The problem will be with the way you're using your config.php.
When your modules entry point file mod_myModuleName.php is loaded by Joomla the $params object is already available in that context, you need to provide it to your scripts.
If you look at something like the mod_articles_latest module you will notice that the helper class is included with this line:
require_once __DIR__ . '/helper.php';
And then helper class is has it's getList() method called statically with the $params passed into it, so that $params is available to class context:
$list = ModArticlesLatestHelper::getList($params);
Inside the helper class ModArticlesLatestHelper you will notice that the getList() expects the $params to be passed in.
public static function getList(&$params)
{
...
}
I would strongly recommend reading the articles in the Modules section of Developers Portal on the Joomla Doc's.
Try the "Creating a simple module" article.

PHP Function Scope Failure

I am struggling to understand scope and what's preventing my new code from working (assuming it is a scope issue).
The following function is in a file PATH.'/includes/custom-functions.php' that references a class:
function infusion() {
require_once(PATH.'/classes/infusion.php'); //PATH is defined in WordPress from ~/wp-content/themes/theme/
return new infusion();
}
The class is reliant on PATH.'/api/isdk.php' and connection credentials from another file within /api/ directory. From within PATH .'/includes/custom-functions.php', I have many other functions that call $infusion = infusion(); and work perfectly.
PROBLEM
I have created a new file: PATH.'/includes/report.php' which I need to access $infusion = infusion();but can't get to work by either repeating the function infusion() definition from above; using require_once();; or using include();. All 3 of those options simply kill the rest of the code and I can only come to the conclusion - well, I have no conclusion.
Any help would be greatly appreciated.
I'm assuming the code isn't using namespaces, therefore you aren't permitted to redeclare the infusion function (either by redefining the function, or re-including the class).
Your includes/report.php file should simply have:
require_once PATH.'/includes/custom-functions.php';
// your other code here ...
$infusion = infusion();
It may be the case that other files / classes that you're including in your file are already requiring custom-functions.php along the line, so you may be able to skip that entirely. Also note that the PATH constant should have already been defined somewhere (either directly or via an included file) before you attempt to use it. If you set your error_reporting to include E_ALL, you'll get a notification in your error log if that constant doesn't exist.
If that fails, your error log(s) may provide some additional background on what your issue is.

Joomla: Call helper function from within a model?

I'm starting off with both php and Joomla development, and finding it difficult working within Joomla to do some fairly simple stuff. Went through the Joomla MVC example and Lynda (and have built a few simple views so far).
I have a helper file/class/function that outputs all the userids that exist in the "completed" table so I can display a link for either a new record based on that user or edit an existing user's record.
I've already used a different function in this helper file successfully in a different part of the component ( Joomla: Write and call a helper function in a component ).
When I do the same thing in the model, I'm getting this: "Fatal error: Call to protected method JModel::_createFileName() from context 'JView' in C:\wamp\www\ilplocal\libraries\joomla\application\component\view.php on line 773". When I try it in the view, works fine - but I need the output in the model.
Code:
lookups.php
abstract class LookupHelper {
public function other_functions($vars){
...
}
public function completions_exist() {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->SELECT(' #__completed.completed_userid as UserID');
$query->FROM (' #__completed');
$query->GROUPBY (' #__completed.completed_userid ');
$db->setQuery($query);
$result = $db->loadResultArray(0);
return $result;
}
}
In the model:
$completions_exist = Jview::loadHelper('lookups');
$completions_exist = LookupHelper::completions_exist();
This line is throwing the error: $completions_exist = Jview::loadHelper('lookups');
I've found some really vague references to something called JLoader::register to pull in helper functions but can't find any good documentation on that in Joomla except for everyone saying to just use that. SO I tried using it like so:
JLoader::register('LookupHelper', dirname( JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php');
$completions_exist = LookupHelper::completions_exist();
which throws this error: "Fatal error: Class 'LookupHelper' not found in C:\wamp\path\to\model\not\to\lookups.php. Tried manipulating the JLoader::register(everything here) and it doesn't effect the path of the error message.
Thoughts? Why does it work in a view and not in the model? How do I use the helper functions within a model?
Thanks!
#####EDIT
Thanks to #cppl looks like it's a path issue with the second bit of code. Also I read that the .DS. notation will be phased out in future versions - so the code that's working is:
JLoader::register('LookupHelper', JPATH_COMPONENT_ADMINISTRATOR.'/helpers/lookups.php');
$completions_exist = LookupHelper::completions_exist();
Lets break this down:
In Joomla! your components helper file should be in `/mycomponent/helpers/lookup.php'
JLoader:: is the Joomla! way to do it, but you could just as easily use PHP's require_once eg. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';
Is your path right? - you're providing dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php' but you've wrapped the path to your component in dirname which will the parent element of the path only. So JLoader is looking in /administrator/helpers/lookups.php.
JPATH_COMPONENT_ADMINISTRATOR is initialised as part of Joomla!'s renderComponent() call in it's JComponentHelper class if you apply dirname to it when it's not setup you will get back a dot (ie. current directory) so in the model you could would be passing ./helpers/lookups.php to the JLoader call.
You can call helper within model by following method:
JLoader::import('helpers.events', JPATH_COMPONENT);
this will include the file helpers/events.php from the component directory.
$_helper = new EventsHelper();
echo $_helper->getAnyInsideMethod();exit;

Kohana 3 and SimpleTest using autorun.php

How do I go about integrating Simpletest with Kohana 3? I have checked out this answer but I like to use the autorun.php functionality from SimpleTest.
After some hours of looking over the code, I have discovered how to do it
Create a new copy of index.php, and name it test_index.php
disable the error_reporting line in test_index.php
Create a new copy of bootstrap.php, and name it test_bootstrap.php
comment out the request at the bottom
Ensure that test_index.php includes test_boostrap.php instead of bootstrap.php
Add simpletests to the directory structure
Write the test case - include 'test_index.php' and 'autorun.php' (from simpletests) and code test cases as usual.
My example:
<?php
include_once ("../../test_index.php");
include_once ("../simpletest/autorun.php");
class kohana_init_test extends UnitTestCase
{
function testTrue()
{
$this->assertTrue(true);
}
function testWelcome()
{
$response = Request::factory('main/index')->execute()->response;
$this->assertEqual($response->content, 'testing');
}
}
?>
Some notes: the $response variable depends on if you are using a View or pure text output. If you are using the template controller or a view, then $response is the view which you have used to render the content. Variables in the view are avaliable , as shown above (the variable content is defined inside the view).

Categories