Laravel - Including Assets in Auth Middleware

In my application, there is a group of routes with a name admin, any route inside this group calls two resources: public/css/admin.cssand public/js/admin.js, but any user who does not pass the test has access to these files. How to include these files in Auth middleware?

My admin routes:

Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function () {
    Route::get('/', 'Admin\IndexController@index')->name('panel');

    Route::group(['prefix' => 'users'], function() {});

    Route::group(['prefix' => 'settings'], function() {});

    Route::fallback('Admin\ExceptionController@exception');
});

My resource links:

http://localhost:3000/css/admin.css
http://localhost:3000/js/admin.js

My resource links should be:

http://localhost:3000/admin/css/admin.css
http://localhost:3000/admin/js/admin.js

If I just create a folder admininside the folder public, I just got 403 error ...

What can i do with this?

+6
source share
4 answers

: .

, css/js, , .

.. git, , . Git Repo

  • 755
  • .
  • .
  • .

:

  • , , .
  • .
  • ( ), .
  • .

  • admin_assets .
  • 755.
  • CommonHelper .
  • :

<link href="{{ asset( CommonHelper::serveAdminAssets('app.css', '/css/') ) }}" rel="stylesheet">

  1. .

, , /, .

CommonHelper :

<?php
/**
 *
 */
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class CommonHelper {
  public static function serveAdminAssets($fileName, $filePath) {

    if( Auth::check() ) {
      $adminAssetsBasePath = public_path().'/admin_assets';

      $source = $adminAssetsBasePath.$filePath.$fileName;

      $destDir = 'public/'.Auth::user()->id.$filePath;

      $dest = $destDir.$fileName;

      Storage::put($dest, file_get_contents($source));

      return Storage::url($dest);
    } else {
      return '';
    }
  }

  public static function removeAdminAssets($id) {

      $destDir = storage_path('app/public/'.Auth::user()->id);
      File::cleanDirectory($destDir);
      File::deleteDirectory($destDir);
  }
}
 ?>

:

  • , , , // . , public/storage, // . Docs

  • , .

+1

, , , css/js. css/js, .

, , PHP. , , .

, .

+5

, .

storage.

, .

Admin Like.

<script>
  {!! \Storage::disk('urdisk name')->get('admin.js'); !!}
</script>

css

 <style>
  {!! \Storage::disk('urdisk name')->get('admin.css'); !!}
</style>

,

+2

auth laravel, , css/js , laravel auth .

P.S , i-e storage/admin.css

​​

Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function () {

Route::get('/', 'Admin\IndexController@index')->name('panel');

Route::get('{file}', 'StaticFileController@serveFile');

Route::group(['prefix' => 'users'], function() {});

Route::group(['prefix' => 'settings'], function() {});

Route::fallback('Admin\ExceptionController@exception');
});

namespace App\Http\Controllers;

use Illuminate\Http\Request;
Use Response;

use App\Http\Requests;

class StaticFileController extends Controller
{
    public function serveFile ($file)
    {

        $storagePath = storage_path($file);
        $mimeType = mime_content_type($storagePath);
        if( ! \File::exists($storagePath)){
            return view('errorpages.404');
        }
        $headers = array(
            'Content-Type' => $mimeType,
            'Content-Disposition' => 'inline; filename="'.$file.'"'
        );
        return Response::make(file_get_contents($storagePath), 200, $headers);

    }
}

http://localhost:3000/admin/css/admin.css
http://localhost:3000/admin/js/admin.js

,

0

All Articles