Laravel project without frontend - php

currently I have a standard Laravel 5 proyect, eg:
Laravel Directory
app Directory
bootstrap Directory
config Directory
database Directory
public Directory
resources Directory
routes Directory
storage Directory
tests Directory
vendor Directory
what I'm trying to do is to take the public and resources folder out from the application and put it them on a different project, and in that way I'll use the laravel part only for backend purposes and the frontend parte I'll manage it on an outside project. e.g.:
Laravel Directory
app Directory
bootstrap Directory
config Directory
database Directory
routes Directory
storage Directory
tests Directory
vendor Directory
Frontend project
index.html
app folder
css folder
assets
Any recomendation or ideas to do it ?

Removing (or moving) the Public folder is not a good idea, especially due to the fact that the public/index.php file is the entrypoint for the application.
I personally use both laravel and lumen for a bunch of my REST-API's and it works great, so that thought is not at all wrong.
Just ignore the views, don't use them and don't expose them from any controller action, but rather return all data from the controllers as JSON instead.
This is easily done from the controller actions like:
public function getSomethingAction() {
return response()->json([
"some" => "property"
]);
}
// Which will produce the following json (including headers and all):
{
"some": "property"
}
I would also recommend that you group your routes under a API namespace of some sort:
Route::group(["prefix" => "api/v1"], function() {
Route::get('something'...
});
So now when you call domain.tdl/api/v1/something you will get a neat json response!

Related

How to prevent Laravel application folders from being indexed by search engines

I tried to key my site's domain into the google search engine and was shocked to realize that the application folders that shouldn't be public have been indexed. Folders and files are like composer.json, a vendor directory, storage directory, resources directory among others.
How I set up:
The application is in the public_html directory, the index.php is the entry point and is in the root directory(public_html)
The other Laravel application directories remain as they are(the default Laravel 7 directory structure)
The application is in the public_html directory, the index.php is the entry point and is in the root directory(public_html) The other Laravel application directories remain as they are(the default Laravel 7 directory structure)
In a default Laravel installation, there is no index.php in the project directory, only a server.php. If you did any change here, please revert them since it would cause a major security issue.
The only Laravel directory that should be publicly accessible is public.
You are using Apache, it means that the DocumentRoot should be set to /path/to/your/project/public (where public is the Laravel public directory).
This way, every files and directories (app, resources...) above this one will not be accessible.
you should move all your project files to root then copy your files from public to public_html then point topublic_html in Laravel:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('path.public', function() {
return base_path().'/public_http';
});
}
Also you can see: How to change public folder to public_html in laravel 5

Laravel elixir version looking in the build directory incorrectly

I'm using Laravel Elixir (5.2) to do some gulp tasks such as concatenating files and then versioning them.
When I version them, by default, they output into the '/build' directory in my public folder. I don't want this to happen- I want them up one level without the build folder.
Looking at the source code for Elixir, I could see that you could pass a directory as another parameter to the version method and the files would be created there.
However, when you try and run your application, it throws an exception as it is looking for the manifest in the build directory.
Line 295 of laravel\framework\src\Illuminate\Foundation\helpers.php has the following function, which is called in the blade template to get the path for the filename of the versioned file:
function elixir($file)
{
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
}
if (isset($manifest[$file])) {
return '/build/'.$manifest[$file];
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
I can fix my issue by removing the hardcoding of the build directory.
2 questions:
1. Is this a bug with Laravel?
2. Is it normal to put public assets into a 'build' directory?
I had the same problem.
You have to have both assets and rev-manifest in the build folder, in order to use elixir helper method.
Even if you create rev-manifest.json with correct paths and store it in build/ so elixir can get to it, it will still create paths to the assets with build/ prefix.
I ended up creating my own helper function to resolve paths from the root folder (basically the same code, without hard coded build/ part)

How to I move my javascript resources into a local folder?

I wrote a small app using laravel 5.2. I tried to keep all of my code in one folder located outside the app folder.
I created a folder called modules. Inside the modules folder I have a folder for vendor name. Then, inside the vendor folder, I have a module folder which contains my code.
Here is a simple folder structure
app modules/vendor name/module name/...
I managed to move my controllers, views and a route file into the module name folder.
How would I move all of the javascripts located in the public folder into my module name folder so that everything inside is in the same folder?
Additionally, I have a lot of questions about the views being in the module folder. Is it a better practice to use $this->publishes() to publish views to the resources/views folder that comes with laravel? If so what are the benefits?
https://laravel.com/docs/5.2/packages
The benefit to publishing the views is that the end user, should they want/need to, then has the option to modify the views.
As for the javascript, its completely fine to contain the javascript in your modules folders as the docs mention. You have 2 options as to how they're then used. You can either choose to publish them which moves them into the public folder, or you can include them in your build process if you use elixir/gulp, etc. See https://laravel.com/docs/5.2/packages#public-assets
Edit:
To publish assets such as javascript files (they could in theory be anything, be that images, css, etc.) use the following from your packages service provider.
public function boot()
{
$this->publishes([
__DIR__ . '/path/to/script.js' => public_path('vendor/my-package'),
__DIR___ . '/path/to/another.js' => public_path('vendor/my-package'),
]);
}
The above will move both script.js and another.js into the /public/vendor/my-package folder. Just to explain __DIR__ is a PHP 'magic' constant which gives the directory the current php file is located in and public_path() is a Laravel function which gives us the location of the /public folder as some users choose to rename this folder to their specific configuration.

Laravel 5 directory structure on shared hosting

I am new to Laravel and started learning v5 few months back. I have created a sample application and want to deploy it over production (Shared hosting).
The easiest way for me is to put all content including app, vendor, config, tests etc folders directly to public_html but I don't want to do this. I want to keep laravel specific things outside the public_html and put only content from public folder to this directory.
What are the options available to do this?
Also, can I share same laravel installation for multiple applications?
on public/index.php add after
$app = require_once __DIR__.'/../bootstrap/app.php';
this:
$app->bind('path.public', function() {
return __DIR__;
});
Now You can change public directory to public_html
Keep your code outside of the public_html and create a symbolic link from public_html to the public directory in your laravel directory.

Integration of svg-edit or other programs on Laravel 4

I'm trying to integrate svg-editor with Laravel 4.
My current solution is to insert svg-editor.html file renamed as svg-editor.php inside my views folder and move the rest of files into the public folder. So now I've moved all the calls to js and css files into the edit view and all points to the public folder to let laravel load the needed files for svg-editor. js and css files are into assets folder and I call them with asset function:
<script>{{{ asset('/assets/js/ *original path from svg-editor* ') }}}</script>
public folder structure
app
public
vendor
svg-edit-2_6
Views folder: inside svg-edit-2_6 there's just svg-editor.php to be able to include on template with #include()
Of course this solution is messy because I've to modify all calls inside svg-editor code such as paths and URLs references. My question is if there's any way to let laravel to load this program without having to modify its params. I think could be possible to make some kind of calls froms start.php file but I could not find information about how to do this.
Could it be possible?

Categories