0

this is my problem for a long year. every time I deployed the project always I add public sub directory to the css / images / JavaScript to the links. i will demonstrate here on the below the comparison.

Question: Is there way not to put the sub directory of public.?

Localhost VS Deployed project on apache.

Localhost:

<script src="{{asset('js/jquery.min.js')}}" crossorigin="anonymous"></script> 
<link href="{{ asset('datatables/dataTables.bootstrap4.min.css') }}" rel="stylesheet">
<img src="{{asset('storage/'.Auth::user()->profile_image)}}" class="img-profile rounded-circle" style="width:40px !important; height:40px !important;">

Production:

<script src="{{asset('/public/js/jquery.min.js')}}" crossorigin="anonymous"></script> 
<link href="{{ asset('/public/datatables/dataTables.bootstrap4.min.css') }}" rel="stylesheet">
<img src="{{asset('/public/storage/'.Auth::user()->profile_image)}}" class="img-profile rounded-circle" style="width:40px !important; height:40px !important;">

Thank you.

SoulAiker
  • 139
  • 8
  • It sounds like the document root on your apache server is not properly set to the 'public' directory. Please see this answer on how to set your Apache root directory: https://stackoverflow.com/a/5891858/11182541. There might be easier ways to set the root directory if you are using a managed service to host your application – Eden Dowling-Mitchell Feb 27 '20 at 04:48
  • @EdenDowling-Mitchell i already point the root directory to the project folder. – SoulAiker Feb 27 '20 at 05:16
  • 1
    And do you point it to the 'public' folder within your project folder? It should be pointing to `/path/to/project/folder/public` – Eden Dowling-Mitchell Feb 27 '20 at 05:22
  • Does this answer your question? [How do I change the root directory of an apache server?](https://stackoverflow.com/questions/5891802/how-do-i-change-the-root-directory-of-an-apache-server) – Niket Joshi Feb 27 '20 at 05:47
  • @EdenDowling-Mitchell why do you mean for that? can you show to me the sample foldering? – SoulAiker Feb 28 '20 at 00:41

2 Answers2

0

No need /public.

<link href="{{asset('css/jquery-ui.min.css')}}" rel="stylesheet"/>
-2

Follow these steps:

  1. Create a new folder in your Laravel Root (in my case _laravel-system).
  2. Move all Laravel files & folders into _laravel-system folder.
  3. Go to _laravel-system\public and move everything to your Laravel Root folder. Leave public folder empty or delete it.
  4. Edit the index.php file in Laravel Root folder.

Edit index.php edit the bootstrap targets.

// require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/_laravel-system/bootstrap/autoload.php';

// $app = require_once __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/_laravel-system/bootstrap/app.php';

Linking Storage

Add this code in app\Providers\AppServiceProvider.php to configure the new public_path.

public function boot () {
    \App::bind('path.public', function() {
        return base_path().'/../';
    });
}

Then run:

php artisan storage:link

If there's an error

The "storage" directory already exists.

Delete your link folder (previously in public\storage) first and run the Artisan command again.


This is my file structure:

"This is my file structure"


NOTE:

Please keep security in mind. Block any access to the Laravel System folder by editing .htaccess file, for example.

vozaldi
  • 818
  • 8
  • 15
  • I agree it will solve the problem but you are breaking the original architecture of the framework. – Kamlesh Jha Feb 27 '20 at 07:20
  • Not breaking the architecture at all, Artisan command and Composer still running well. Laravel has flexibility to adjust our needs. – vozaldi Feb 27 '20 at 07:41