Exporting a Laravel project within a Laravel project
I am building some kind of landing page generator which is based on laravel. The user first inserts a new 'industry', then chooses his preferred layout and then he'll be able to simply click 'download' next to his created website and it should download him a whole zip including the chosen layout (already split to views under the /views folder) and the right db migrations according to the industry he chose.
The output website will include kind of a simple CRUD so the db can be populated quickly, and all the routes will be generated dynamically according to the db of course.
Most of the project is already done - but while approaching to this export issue, I realized that I really -don't- know how to handle it.. Sorry in advance for not including some code, but I am trying to figure it out conceptually.. Am I supposed to hold an empty laravel project in one of my folders and then when the user clicks 'download' it simply generates the preferred views (According to the chosen layout), and the default routes/controllers according to the project - and then I should copy(?) them into the empty laravel project folder and let the user zip and download the whole folder? Or is there a more effective way to do it?
Well I think you already have the right concept. Ideally you would like to have a copy of the latest Laravel or a specific version and copy over the correct files to a temp location. So first copy the Laravel project to a temp location and then add the correct views/assets etc. After that you can simply create a zip and return that.
Another solution is using something like caching. So instead of copying the Laravel project and the template specific files every time, you simply create a directory per template. You can even already zip this directory, so you only need to return the zip. This last option completely depends on how many options you have and if the templates are really unique or kinda the same!
Great, thanks. I think I'll go with 'holding a copy of the latest Laravel' approach.
And actually I do have another question - What would be the best approach to create an SQL file as a result of the user's input in my landing page generator?
I'll elaborate.. When creating the landing page - it should export the whole laravel folder with a crud system in it which is based on the industry that the user chose while creating this page.. If I chose 'Real Estate' - It should export me a specific layout with an admin panel where you can insert new 'operators' and the route to these operators is already defined dynamically in web.php. This crud is based on specific DB columns that should be exported as well..
my question is - What would be the better approach to create this SQL file when the user hits 'export'? Should i simply write to it as a text file and save it with a .sql extension? Or should I really create these tables within the system and then somehow export it using some plugin?
Well Laravel has an awesome system for databases in the form of migrations. What you can do is use a separate directory with migrations per type of exports. This way you can add migrations later on for newer versions and you can also update the database of older application by simply running the migrations
You can run migrations from another directory by using the --path option. For example
php artisan --path=database/migrations/template1
Now you have all the benefits of migrations and the flexibility for generating projects ;)
An alternative solution is using .sql files, however you need to keep these updated as well which might be a harder because it's a huge file!
But I need to create these migration files on the fly - according to the columns the user selected while creating the website. Is there some library in laravel that functions as a helper to create these migration files?
I'll give you an example - I have a form with a text box, and the user writes there "name,description,pros,cons,total_points" (leave security aside at this moment..), when he hits 'export' - this migration (code is added below) should be added to this fresh updated copy of laravel and then should be downloaded with laravel's folder as a whole.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateIndustryMigration extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('website_industry', function (Blueprint $table) {
$table->string('name')->index();
$table->string('description');
$table->string('pros');
$table->string('cons');
$table->string('total_points');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('website_industry');
}
}
As far as I know there is no such functionality available in the framework for that. There might be some package that can help you out, but I never used any so I can't recommend any.