How to store templates for non-php files in Laravel?

…衆ロ難τιáo~ 提交于 2021-01-27 01:36:18

问题


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.


回答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).




回答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



来源:https://stackoverflow.com/questions/49044898/how-to-store-templates-for-non-php-files-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!