Zend 2 :: easy to get path to shared folder or basePath () in controller action

In my application, to move a file to a specific directory, I need to know the path of the public folders in the controller action. I read a different solution of this type, but did not get easy access. I know that we can easily find the path to shared folders with $this->basePath(); view helper. I definitely want this to be in controller action. Anyone can help me how I can achieve this. Thanks in advance.

+6
source share
5 answers

index.php sets the current working directory to the root directory of the application (a folder containing composer.json , init_autoloader.php , etc.)

Until you call chdir to another place in your application, you can call getcwd() and it will always return the path to your application root.

Since the shared folder is relative to this, you can get the path using ...

 $publicDir = getcwd() . '/public'; 
+12
source

In your shared folder, edit the file named index.php add only two lines

 define('BASE_PATH', realpath(dirname(__DIR__))); define('PUBLIC_PATH', BASE_PATH.'/public'); 

you can use in your code for example

 print_r(BASE_PATH); print_r(PUBLIC_PATH); 
+2
source

You can use view helpers from the controller in ZF2, as shown here here and here . You can try this for your case:

 $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); $url = $renderer->basePath('the_ressource_you_want_to_get_from_public_folder'); 

Hope this is what you are looking for!

0
source

If you want to include the file from the public folder (regardless of the location of the index.php file): include_one ("./public/your-file.php");

0
source

You should try this if you want to create a shared folder:

 $publicPath = $_SERVER['DOCUMENT_ROOT']; 

Or try if you want the base path:

 $basePath = dirname($_SERVER['DOCUMENT_ROOT']); 
0
source

Source: https://habr.com/ru/post/969461/


All Articles