How to access a file from storage in Laravel 5.8 - php

I have a file inside my storage folder.
Located path :
storage/app/public/$/10012940/gallery/289sdas98e.jpg
$, 10012940 and 289sdas98e.jpg is dynamic and can change.
Route::get('storage/{$folderDirectory1}/{$customerReference}/gallery/{file}', function ($filename)
{
});
Since its a nested folder I cannot find much on internet as to what the route should look like. Also how to go about doing something like that.
I am trying to find a way to access this path via the route so that I can write a middleware to authenticate before making this accessible to the end user.
I am storing the file inside the storage folder due to the visibility. I would like to control who should have access to viewing the image

You should use a route name.
For instance, in your view: href="{{ route('assets.show', $asset) }}".
In your AssetController#show, simply return Storage::download($asset) for instance.
Then use your middleware to authorize or not the access to the show method.

Related

Shorten URL codes using middlewares in Laravel

I still want my codes in public directory, and since I have to use the school's computer, I can't make the URL from localhost to something else.
Example: whenever I type URL for href, I have to put it like this. (larsamp is my project name, posts are the param)
About
But I just want to type this instead
About
Here is the routing PHP:
Route::get('/about', 'PagesController#about');
I found some solutions, but they do not work.
Change public directory to shorten the public part: I have another directory called private to store many things else.
Create a global variable (i.e. $u) and put it in the layout directory: but I have many layouts.
Overload href or make a function inherit href and use it instead: my teacher said that will make my program too complex. Not good.
In conclusion, a way to shorten URL without making the program too complex and can be used on a school computer (no changing host.txt)
You can use the url() helper which returns a fully qualified url:
<a href="{{ url('about') }}>About</a>
https://laravel.com/docs/5.8/helpers#method-url
Alternatively there is also the route() helper:
Route::get('/about', 'PagesController#about')->name('pages.about');
<a href="{{ route('pages.about') }}>About</a>
https://laravel.com/docs/5.8/helpers#method-route

Laravel #extends() pointer system

Laravel 5.7.
If I navigate to a page that does not exist, I get 404 error page handling.
That view is located in vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/404.blade.php
However this file extends:
#extends('errors::illustrated-layout')
This is located in the same folder, and is named illustrated-layout.blade.php
So I guess that the errors:: part points to the specific folder., e.g. vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/
Question: Is this type of pointer something that could be created manually, so a person wouldn't have to write the entire path to a specific folder, when extending a view? Would make things much more clean.
You can add a view namespace and achieve the same result.
For example, you can add the following in AppServiceProvider#boot:
$this->app['view']->addNamespace('admin', base_path() . '/resources/views/admin');
and let's suppose you have a blade file in resources/views/admin/layouts/master.blade.php
you can access it with admin::layouts.master

Laravel Forwarding Route To Another Route File

I'm building enterprise modular Laravel web application but I'm having a small problem.
I would like to have it so that if someone goes to the /api/*/ route (/api/ is a route group) that it will go to an InputController. the first variable next to /api/ will be the module name that the api is requesting info from. So lets say for example: /api/phonefinder/find
In this case, when someone hit's this route, it will go to InputController, verifiy if the module 'phonefinder' exists, then sends anything after the /api/phonefinder to the correct routes file in that module's folder (In this case the '/find' Route..)
So:
/api/phonefinder/find - Go to input controller and verify if phonefinder module exists (Always go to InputController even if its another module instead of phonefinder)
/find - Then call the /find route inside folder Modules/phonefinder/routes.php
Any idea's on how to achieve this?
Middlewares are designed for this purpose. You can create a middleware by typing
php artisan make:middleware MiddlewareName
It will create a middleware named 'MiddlewareName' under namespace App\Http\Middleware; path.
In this middleware, write your controls in the handle function. It should return $next($request); Dont change this part.
In your Http\Kernel.php file, go to $routeMiddleware variable and add this line:
'middleware_name' => \App\Http\Middleware\MiddlewareName::class,
And finally, go to your web.php file and set the middleware. An example can be given as:
Route::middleware(['middleware_name'])->group(function () {
Route::prefix('api')->group(function () {
Route::get('/phonefinder', 'SomeController#someMethod');
});
});
Whenever you call api/phonefinder endpoint, it will go to the Middleware first.
What you are looking for is HMVC, where you can send internal route requests, but Laravel doesn't support it.
If you want to have one access point for your modular application then you should declare it like this (for example):
Route::any('api/{module}/{action}', 'InputController#moduleAction');
Then in your moduleAction($module, $action) you can process it accordingly, initialize needed module and call it's action with all attached data. Implement your own Module class the way you need and work from there.
Laravel doesn't support HMVC, you can't have one general route using other internal routes. And if those routes (/find in your case) are not internal and can be publicly accessed then also having one general route makes no sense.

Locate bower_components in laravel and using loadbalancer

I would like to know how can I call all my scripts file inside /public/bower_components in laravel? I'm having problem with the loading of all my scripts since we are also using a loadbalancer. Here's a sample of my code
URL: http://10.0.2.3/transaction/ <---- my project url where 10.0.2.3 is our loadbalancer/haproxy public IP and 'transaction' is the keyword use to reroute to our private server.
Now when I use this laravel script
{{ Html::script('bower_components/angular/angular.js') }}
I assumed that this will look for the angular.js file in the url
http://10.0.2.3/transaction/bower_components/angular/angular.js
but upon checking my file its looking in this url
http://10.0.2.3/bower_components/angular/angular.js
as you can see the keyword 'transaction' is removed. Is there a way to fix this so that it will look for the file in the correct path with 'transaction' included in the url?
You can just write a custom helper to prepend the route
if(! function_exists('asset_lb') {
function asset_lb($path) {
return asset('/transaction/'.$path); // <--- you need to customize the PATH Separator.
}
}
then use in project
{{ asset_lb('bower_components/angular/angular.js') }}

Laravel routing and route parameters

I have route defined in my route.php file like below:
Route::get('{test1}/{test2}/{test3}', function($test1, $test2, $test3) {
$result = [$test1, $test2, $test3];
return view('view', compact('result'));
});
it works fine in my controller but on the view part when i see it in the browser when i just write something like this in browser:
http://localhost/mysitefolder/public/test1/test2/test3
it loads the view and pass all the data but it gets all of my assets like my stylesheets, images and scripts from a url like below:
http://localhost/mysitefolder/public/test1/test2/js/jquery.js
why is that happing?
thanks in advance!
Two solutions either use a / in the start of your URLs so that relative addressing is not used or use laravel's helper functions {{ asset('/js/script.js') }}.
When you do not use '/' in the beginning your url, it is treated as if it was a relative address and current location is added in its start.
Sometimes even '/' won't work if your application is not served on Root level. For example you have your application at http://localhost/yourapplication then / would refer to your localhost instead of application, that's why best way is to use laravel's helper function.

Categories