We can store PHP template files using blade templating engine in Laravel. But, I want to create a config file on a remote server having more than 20-30 lines each.
Till now, I was doing this using Perl. I used to execute Perl file that used to dump contents in one file and I used to pass variables as parameters.
Now, I want to do it without using Perl. I tried looking for a solution but failed. To make it easy to understand, Here is what I am trying to do exactly!
I want to create the following config file on a remote server (Just an example).
Example.conf
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com </VirtualHost> Here, example.com and www.example.com will vary in every config file.
Now, I want to create this file from my laravel application to the remote server. Is there any way I can store template of this config file and can compile and put file on remote server?
I know how can I put this file on Remote server. I just want to know the best possible way to store template and customize it when needed.
3 Answers
Answers 1
You can put it into blade template like server-config.blade.php and then when you want to place it on a a server you just call:
\File::put('place-on-the-server.conf', view('server-config')->render()); which will generate content based on the blade template (so you can pass variables to this template).
Answers 2
As of my understanding, is that in your senario is that you have a website where people can request websites and you want to auto-generate the virtual-host file inside your application and then transfer this to the remote server?
May the above be the case then you have a lot of options.
1 - The answer of Filip
2 - Console commands
You can generate your file anyway you like. I personally have a structure in my laravel applications where I save blank files (templates). I then copy it over, and replace it with given parameters over console command, Artisan Commands
3 - Server scripts
Another way could be using Laravel Envoy. You will have a script on the server that will generate the file needed, and just call a function in your Envoy file of laravel to have it execute it. The good part about this is that you can make a new user on the server specifically for envoy and only allow it to run specific commands. This way theres very little way of messing up a server.Envoy
Answers 3
I have done something similar previously for Laravel by just creating regular function in PHP.
So what I did is having a template like the one you have and call it Example.conf where it is accessible:
<VirtualHost *:80> ServerName example ServerAlias www.example </VirtualHost> Than create a function in your (business logic) controller that reads -> modify -> save the file, some thing like:
$file = "./Example.conf"; modifyConfigFile("MyNewDom.Net", readConfigFile($file)); function readConfigFile($file) { $myFile = fopen($file, "r") or die("Unable to open file!"); $content = fread($myFile, filesize($file)); fclose($myFile); return $content; } function modifyConfigFile($domain, $content) { $myFile = fopen("$domain.conf", "w") or die("Unable to open file!"); $txt = str_replace("example", $domain, $content); fwrite($myFile, $txt); fclose($myFile); } This will result in creating new file called MyNewDom.Net.conf with following content:
<VirtualHost *:80> ServerName MyNewDom.Net ServerAlias www.MyNewDom.Net </VirtualHost> Than you can transfer it to server as you mentioned in your question.
The code here is just for inspiration and not intend to be final solution, you can make your modification to final fit your project.
0 comments:
Post a Comment