Global var accesible only by redeclare with "global" - joomla

I have Joomla and some external php scripts and libraries. There is a main library where I declare some global vars.
lib.php
global $var_glb; $var_glb="some_value";
script.php:
require_once(lib.php);
print_r($var_glb) //Notice: Undefined variable
print_r$GLOBALS['var_glb']); //DEFINED!
global $var_glb; print_r($var_glb) //DEFINED!
So I had to put "global" in front of $var_glb in order to be accessible... normally it should be defined without "global"...
I don't know why $var_glb (without "global") is lost and only $GLOBALS['var_glb'] is accessible or when using "global $var_glb"
Why is that happening? I don't want to redeclare all global vars in the script.

I used Jumi component in order to insert php scripts into Joomla.
And the problem with global vars was related to Jumi.
I tried another similar component called Sourcerer and now everything is working ok.

Related

Why calling env() function directly in controllers does not work?

Supporting Laravel 6.20 app, I see a lot of env() methods called in the controllers.
It does not work on my side, so I made a wrapper in the config/app.php file, and using it made it work.
And I have to make a lot of similar changes in the app to make it work properly...
But looks like that env() worked for my client on live and developers server.
I know that calling env() in controllers directly is a bad way but wonder why it worked on servers?
Are there some common PHP configurations?
Thanks!
you have probably cached your configurations by running php artisan config:cache
according to the doc
This command will combine all of Laravel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.
and then
If you execute the config:cache command, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function for .env variables will return null.
you have to just run php artisan config:clear and this will clear the cache file. you will be able to get the env values again.
but yeah don't use any values directly from .env files. there are some issues. to get more performance, cache the configs. so wrap the env value in a config key like in config/app.php
'name' => env('APP_NAME', 'Laravel')
you are saying that use the APP_NAME key from the env file as your laravel app name and if there's no APP_NAME key or value in env use Laravel as default.
secondly if you make some requests within a very short period of time, like page loads and you are calling some xmlhttp requests at the same time, there's possibility of not getting the env values (returns null) if you have to use any. this is a php bug. so read env values from config files with a default value for performance and for avoiding bug.

Include a jade file with the use of a session variable

I am developing a web application that have 3 different acctypes.
Student teacher and admin.
Depending on what acctype is logged in I wan't to load different menus.
I have acctype assigned to a session variable;
#{session.acctype}
So I used to handle it like this;
script(src='js/sidebar#{session.acctype}.js')
But I wanted to get rid of the use of document.write that I used in the js file that was loaded above.
So now I have the menu in a jade file inside.
Here comes the problem, I don't know how to use the session variable to load a jade file!
I tried this without any success;
include sidebar#{session.acctype}.jade
So, is there any way to handle this?
In order to give your templates access to your session variables, you need to add them to res.session.locals. An easy way to accomplish this is by adding
app.use(function(req,res,next){
res.locals.session = req.session;
next();
});
to your application before you call app.use(app.router). This technique is called using middleware.

Where to declare application scope variables on marionettejs I

I am using marionettejs yeoman generator
https://github.com/mrichard/generator-marionette
what's the best way to declare global variables like application root path and API endpoint ?
Declare them right on the app itself, or create a Util module or something. There are numerous ways to do it.

How to view drupal 7 sessions from an external PHP page?

I installed drupal 7 and I need to include a block of PHP code in the user profile page.
I created a new block and I put this code in it:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
In "campi_aggiuntivi.php" I put the following code:
session_start ();
print_r ($ _SESSION);
But it returns an empty array.
How do I interact with drupal sessions in this external page?
Thank you very much for your answares.
Thank you all for your answers.
I'm trying to install a module and insert it into a block avege as I suggested.
I created the file test.info
name = Test Block
description = description Test Block
package = package Test Block
core = 7.x
dependencies[] = block
; Information added by drupal.org packaging script on 2013-02-01
version = "7.x-1.x-dev"
core = "7.x"
project = "Test Block"
datestamp = "1359680350"
But I can not figure out which function I put in the file test.module, to insert the test module management screen Blocks
Can you help me please?
Thank you again to all
I think you need to include Drupal Bootstrap and a few other necessary procedures. Here's a link to an issue on d.o that might help.
1st thing i want to suggest is don't include like this:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
Use local path instead: "include (sites/all/themes/campi_aggiuntivi.php)"
But this depends on from where you are calling, if both the files are at same location(calling in user profile tpl file) then this might work: "include (campi_aggiuntivi.php)"
I presume you need session details to figure-out the user id, that information can be retrieved from $profile variable or arg(1)
I have not tested your code but the way you are trying to do is not the conventional way to extend Drupal functionality.
Also i hope print_r ($ _SESSION); is actually print_r ($_SESSION); in your code, i.e. without spaces.
If you want to add a block in user profile page, i recommend doing it the drupal way.
Drupal already has an excellent example for creating a block programmatically. Here is the link.
Have a look at block_example module inside it. It will give you relevant example codes that you can use to create the block.
The example also contains information about how to make the block appear in certain pages by default.
After you create the block place them in the region you would want to, like left sidebar, content etc of the relevant theme that is enabled on user profile pages.
Inside your custom module you would access to $_SESSION variable.
From here on you can include the code in campi_aggiuntivi.php inside your module itself.
Or use the following foolproof method to load the file. This would even work when you call it from a theme.
Say you want to use custom_module_name.campi_aggiuntivi.inc located in your custom module, from a theme then you can use module_load_include
For example,
module_load_include('inc', 'custom_module_name', 'custom_module_name.campi_aggiuntivi');
the above call would load custom_module_name.campi_aggiuntivi.inc from custom_module_name module folder. This way you don't have to hard code the path required to access the file.
Based on your update.. it sounds like you are now trying to create a block programmatically. There are example modules that you can download on Drupal.org. There is a block module in that download.

CakePhp2 defined CAKE_CORE_INCLUDE_PATH, now DS and CORE_PATH undefined in ShellDispatcher

In order to share CakePhp2 core between many sites I edited CAKE_CORE_INCLUDE_PATH in webroot/index.php to point to the cake directory. That works and I can reach my welcome page.
However, when I attempted to run the Cake Console, I ran into issues with ShellDispatcher.php not defining DS and CORE_PATH because they were only defined if CAKE_CORE_INCLUDE_PATH was NOT defined. Once I defined them out of the if statement which checks if CAKE_CORE_INCLUDE_PATH was not defined, I was good to go. However, I would prefer not to "hack" this file because I want to keep the cake core files clean. Are there any better and cleaner options?
I also had to defined CAKE_CORE_INCLUDE_PATH and use it to set $dispatcher in Console/cake.php which is part of the app of course.
Yes there are. As a start never define application-wide variables in Webroot/index.php. As a matter of fact you shouldn't be touching this file at all. You can define variables in
Config/bootstrap.php.
You were getting the error because when in a Shell you're not firing Webroot/index.php at all - you're actually running PHP's CLI and CakePHP fires off in a different manner.
It will go through the bootstrap.php file of course.
You should also use this file to define any global vars, but there is a little bottleneck
here when using PHP's $_SERVER variable's web server related members - e.g. HTTP_HOST and the definition: $myHost = $_SERVER['HTTP_HOST'];. If you start a shell Cake will try to set this variable and it will fail throwing an error. This is because as mentioned before PHP will be running in CLI mode when the CakeShell is envoked. There is a way to detect this of course - you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['SCRIPT_NAME'] in bootstrap.php to identify if you're in CLI or not. :)

Resources