Get image folder URI from controller and twig template - php

I moved all the public images of my website to a folder out of the web folder of symfony so it can be shared with other web applications.
I created a symbolic link to this folder so it can be accessed from the web folder of the symfony application.
ln -s /absolute/path/to/images /path/to/symfony/application_01/web/images
ln -s /absolute/path/to/images /path/to/symfony/application_02/web/images
Etc... for all applications.
I'm looking for a method that would allow me to retrieve the images URI in my development environment on localhost as well as in my production environment on the remote web server.
I would like to be able to retrieve this URI from the twig template AND from the controller.
Basically (for my application_01 for instance):
In the development environment it would return:
http://localhost/images/my_image.jpg
In the production environment, it would return:
http://www.application_01.com/images/my_image.jpg
My problem is that I found lots of different ways to get URIs but I'm not totally clear about how Symfony manages them and what functions to use to have a global solution working in all cases.
What is the best way to achieve my goal?
EDIT
Specifically I found some SO answers to quite similar questions proposing to use the following functions:
$request->getScheme();
$request->getHttpHost();
That seems to correspond to the values or superglobals $_SERVER['REQUEST_SCHEME'] and $_SERVER['HTTP_HOST']
And I don't know if this is the proper way to get this information since this is linked to the current request received by the server.
Isn't there another way to get this info independently from the request currently processed by the server?

Define the following url
image_url:
path: /images/{image}
Then use it like this
/** #var Router $router */
$router = $this->get('router');
$url = $router->generate('image_url', ['image' => 'my_image.jpg'], Router::ABSOLUTE_URL);
$url now contains the absolute url to your image. And of course it's the best practice you can think of :)
----- Answer to your comment about app_dev.php
We used this solution in our company months ago.
preg_replace('#/(.+?)\.php)', '', $router->generate('my_url');
http://techimho.com/index.php/2015/10/15/generate-production-url-in-dev-environment/

Related

Using absolute paths in Laravel with wamp

I'm developing a Laravel project using wamp stack on windows. My project is located in a separate folder like C:\wamp64\www\[project name]. The annoying problem is with url paths in code. I want to handle them in a way that they work both locally and on production environment.
For example this an absolute link:
<a href="/posts/tags/{{ $tag }}">
It is intended to navigate user to [project name]/posts/... . In other words I want to get project root with a slash. If this is not possible, what is the correct way of handling paths then (on development and production environment). I'm a little confused with this. Please provide detailed information considering both WAMP and Laravel. And please give information about relative paths, too.
You can use url method url method will return base url
edit tag
url()
The url function generates a fully qualified URL to the given path
So what I finally did was to use the suggested method by #iCoders plus having APP_URL set in .env file to http://localhost/[project_name]/public.
This way url method resolves the correct path provided that your config/app.php contains 'url' => env('APP_URL', 'http://localhost') and you make the URL generator use APP_URL. For more information, check this link:
Laravel: Change base URL?
To differentiate development and production environments different .env files can be used for each.

Testing multi-tenancy Laravel applications using Behat / Mink and Behat Laravel Extension

I am building a multi-tenant SaaS application which I am trying to write tests for with Behat, using Mink and the Behat Laravel Extension
When you register for an account, you get your own subdomain on the site {account}.tenancy.dev
my behat.yml file looks like so:
default:
extensions:
Laracasts\Behat:
# env_path: .env.behat
Behat\MinkExtension:
default_session: laravel
base_url: http://tenancy.dev
laravel: ~
I am having problems straight off the bat as when I try to test my registration flow, I am getting a 404 error testing that the new subdomain is accessible, all of the data has been saved correctly, manually testing the process works and the subdomain routing works.
I was wondering if there was any way to do this using Behat and how I would go about setting Behat / Mink to use wildcard subdomains to test SaaS applications?
I am running the test inside the Homestead VM.
The base_url: http://tenancy.dev configuration is used to generate a fully qualified domain URL when you utilize relative path URL's in your mink steps (IE "/home").
When you want to hit a domain different from the domain specified in base_url, all you have to do is use the fully qualified domain URL in your step like "http://test.tenancy.dev/fully/qualified".
So use the base_url configuration to set what you will be using for the majority of your steps as relative url's and then explicitly specify the full domain for the exceptions.
When I create an account named foo
And GET "http://foo.tenancy.dev/ping"
Then I get a 200 response code
When I GET "/home"
Then the response contains "Sign Up"
If the majority of your testing will be against the sub domain, set that as your base_url and explicitly specify your top level domain when necessary.
You may resolve subdomains using xip.io, which is especially useful if you cannot access the /etc/hosts file on a CI server, for example.
To route {account}.tenancy.dev to your local webserver, you can use account.tenancy.dev.127.0.0.1.xip.io which resolves to 127.0.0.1.
After a short while I revisited this problem and found a rather simple solution to be used in my FeatureContext.php:
$this->setMinkParameter('base_url', $url);
This changes the base url for any scenario it is used in:
/**
* #Given I visit the url :url
*/
public function visitDomain($url)
{
$this->setMinkParameter('base_url', $url);
$this->visit('/');
}
Which is used in the following way:
Scenario: Test Multi Tenancy
Given I have a business "mttest"
When I visit the url "http://mttest.example.com"
Then I should see "mttest"
Obviously this is slightly contrived but does show that what I was intending to do is possible.

PHP ZF2 - multiple sites with separate public folders

I am trying to create a single ZF2 installation with multiple websites under it, with each site being a separate module. So far, with using Hostname routing, it works, except that all of the sites share the same public folder.
Is there a way to configure each module to have its own public folder?
I have seen some other questions about this, but they have mostly dealt with the routing itself or been ZF1 specific, which kept the public folder within the module.
You can quite easily do what you want:
site1.com document root: ~/project/site1/public
site2.com document root: ~/project/site2/public
~/project/site1/public/index.php and
~/project/site2/public/index.php both contain:
<?php
// Set time zone.
date_default_timezone_set('Europe/Paris');
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
define('ROOT_PATH', dirname(__DIR__.'../'));
chdir(dirname(__DIR__.'../'));
// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
return false;
}
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require '../config/application.config.php')->run();
You can have as many public folder as you want. Just put in index.php valid include of zf2 core and everything should work without any special modifications (what module to display can be specified by domain path or via global configuration you will put inside index.php).
Still I think is perfectly fine to have one public folder and change active modul per domain, also use some asset manager with configuration and content encapsulated inside module. Best for this is rwoverdijk/AssetManager.

Laravel Application Environment - Best practice and security advice

I am planning on building a multi-tenant application in Laravel with a master subdomain holding the relevant public files and a subdomain for each customer who will have their own databases pointing to the 'master' files. I am planning on doing this as automated as possible e.g. you click a button a subdomain is created, a database is created and the relevant config files are set. All this is fine except I'm not sure if my practices are the best to use and whether or not there are any security issues with it.
In the bootstrap/start.php file I have the following:
$env = $app->detectEnvironment(array(
$_SERVER['SERVER_NAME'] => array($_SERVER['SERVER_NAME'])
));
This would essentially mean that the environment for test.example.co.uk is test.example.co.uk. My install script will create a config directory 'test.example.co.uk` in 'app/config' and will add the relevant database config there.
This does all work as I expected so I am just looking for advice, are there any vulnerabilities with this?
Just to Add - Users will not be able to use the installation script, its just for the developers
I don't think there is any security issues with your code. One thing that I notice is that you are limiting youself to just one environment. Here is my env settings:
$env = $app->detectEnvironment(function()
{
return getenv("ENV") ? : "local";
});
Now my environment will be auto detected - on server I did provide "hook"
in the form of getenv function, and on local machine it is local.
Also instead of array, I am sending callback to detectEnvironment - for more flexibility.

Zend Framework 2 real world application file structure

I have just started out with ZF2 and i am very confused with Zend Skeleton Application.
In current situation URl looks like:
http://localhost/zf2/public/
And for a module named Application it looks like:
http://localhost/zf2/public/Application/
and the actions goes after the module name.
i want to create a CMS with admin panel and users panel.
And that's why I want my URL for users to be like:
http://localhost/zf2
and for admin like:
http://localhost/zf2/admin/Module Name/Actions
So, my question is, How am i supposed to create URL like this?
Your url examples look like you are confusing the public directory and the controller routes. You should usually not have a url like this:
http://localhost/zf2/public/
Instead you should generally be using a vhost. There are numerous ways to do this, but generally it boils down to either a custom port or a custom hosts entry if you want a named vhost. Then your url to public will look like one of these two options:
http://localhost:9000/
or
http://myapp.local/
If you are using PHP 5.4, in your development environment, by far the easiest way to start a host is to use the PHP 5.4 built-in server. You start that up like this on the command line from your project root (this makes the public directory the web root of the temporary web server on port 9000 of your localhost):
php -S localhost:9000 -t public
Once you have your web server configuration sorted out, the Skeleton app will automatically interpret your url routes (by default) like this:
http://localhost:9000/some-module/some-controller/some-action
If you want to put in the full literal path the the default indexAction on the IndexController in the Skeleton app, it looks like this:
http://localhost:9000/application/index/index

Categories