Pass $basePath parameter to service setup method in config.neon

Notice: This thread is very old.
VasaB
Member | 8
+
0
-

Hi all,

maybe I miss something, but currently I'm a little bit stuck on implementing texy filter, which will correctly handle urls to images.

My texy source file contains: [* something.jpg *]

What I want to achieve as result is correct path to image in form /$basePath/images/something.jpg. The $basePath parameter is the part, I'm not sure how to make.

I register Texy as a service in my config.neon:

texyFilter:
    class: App\Filters\TexyFilter
    setup:
        - createTexy(%imagesDir%, /* $basePath should go here */)
nette.latteFactory:
    setup:
        - addFilter('texy', [@texyFilter, process])

So my question is how can I pass $basePath parameter to setup method. Or if it's not possible I'd like to know, how you handle generating correct image paths via Texy.

Thanks a lot for any answer.

Namespace
Member | 80
+
0
-

Hi,
in config.neon is avalible %wwwDir%

VasaB
Member | 8
+
0
-

Yes, but %wwwDir% is absolute file system path, but I need relative uri to put it to image src attribute. So basically I need an alternative to $basePath which is available in templates.

Namespace
Member | 80
+
-2
-

No, absolute system path is in %appDir% :). I use %wwwDir% in models normally.

for example:

<?php
services:
	- App\Forms\UploadFormFactory(%wwwDir%)
?>
<?php
public function __construct($dir)
	{
		$this->dir = $dir;
	}

public function onSuccess($form, $values)
	{
		$path = $this->dir . "/upload/" . $values->file->getName();
		$vals = [
			'file' => $values->file->getName(),
			'users_id' => $values->users_id,
		];
		$tmp = $this->manager->save($vals);
		if ($tmp != FALSE) {
			$values->file->move($path);
		}
	}
?>

Last edited by Namespace (2015-09-16 21:17)

David Matějka
Moderator | 6445
+
+3
-

Hi, you can inject Nette\Http\IRequest and create base path from the url. See the template factory for the inspiration: https://api.nette.org/…ory.php.html#… :)

@Namespace %wwwDir% is a system path to the www dir, $basePath is the base path in the url

VasaB
Member | 8
+
0
-

@DavidMatějka Thank you! That's exactly what I need :)