Redirect from a file in Laravel 8 - php

have the following problem, and is that I need to redirect from a link which is created by me with 6 random characters to make a shortener to a file. The problem is that when I try to access the file I get the url http://127.0.0.1:8000/C:/Users/myUser/Desktop/proyect/public/assets/PDFs/file.pdf but I do not need this url, but the following, which would be from C://.
My doubt is, how could I do this? Since I can't find anything that can solve it and I have been trying for days to find the problem and trying in other ways, even with:
Storage::disk('local')->get($qr->document)
The blade code is as follows:
<a class="button shortlink" href="{{ route('shorten.linkDocument', $qr->code) }}" target="_blank"> <i class="fa fa-link"></i>
{{ route('shorten.linkDocument', $qr->code) }} </a> <br>.
Where it says $qr->code are the 6 random characters generated to act as a shortener and redirector. The route to shorten.linkDocument is found in Web, and is as follows:
Route::get('Pdf/{code}', '\App\Http\Controllers\QrController#shortenLinkDocument')->name('shorten.linkDocument');
The method referenced by the controller is as follows:
public function shortenLinkDocument($code).
{
$find = Qr::where('code', $code)->first();
return redirect($find->document);
}
PS: In $qr->document the absolute path from C:// is stored.
I hope for your help and thank you very much if you have read everything.

Related

Why I am getting wrong URI?

I am currently on
http://127.0.0.1:8000/cars/
I have a link that has to route me to
http://127.0.0.1:8000/cars/create
I used this in my code
<a
href="cars/create"
</a>
In my web.php I have following route
Route::get('cars/create',function ()
{
return view('carsops.create');
});
When I click on link I am redirected to
http://127.0.0.1:8000/cars/cars/create
Instead of
http://127.0.0.1:8000/cars/create
What is the error why I am getting this extra /cars.
Can some one help me.
You have to put a / in front of the link:
<a
href="/cars/create"
</a>
This means Go to the site root, then got to cars path then go to the create path.
This is solution is not nice. Why? Ok, you should use route names in the your application.
How to solve this problem correctly?
A first step is set name to your route. Modify routes/web.php like to
Route::get('cars/create',function ()
{
return view('carsops.create');
})->name('carsops.create');
Look at the name. Name can be as you wish.
Step 2 is, calling your route in the blade like to
<a
href="{{ route('carsops.create') }}"
</a>
Well done.
Laravel a href links are made by giving name,
if you want a return view without variables you don't need to get a method
Route::view('cars/create','carsops.create')->name('yournamelink');
use in blade page
goto page
A root-relative URL starts allway with a / character, to look something like create car.
The link you posted: create car is linking to an folder located in a directory named create in the cars, the directory cars being in the same directory as the html page in which this link appears.
It is therefore easier to work with Laravel Helper. As already mentioned here.
<a href="{{ route('car.create') }}"</a>
You define the route names in your web.php and for api links in your api.php file.
Route::get('/create-car', [App\Http\Controllers\PageController::class, 'create'])->name('car.create');

How to remove a PATH in PHP [duplicate]

This question already has an answer here:
How to remove a PATH [closed]
(1 answer)
Closed 2 years ago.
I just got a php script for ebooks.
This is the script https://codecanyon.net/item/ebookshare-ebook-hosting-and-sharing-script/23888795
The problem is that I want to keep the script on one site and the covers/books on another hosting.
The script allows me for the books to put and external URL but not for the covers.
If I try to put the external link in the database, it looks like the script puts a path in front.
How could I remove that path? ( http://localhost/eBook/public/storage/http://mywebsitestorage.com/picture.png )
Thank you
CODE:
images.blade.php
I'm not 100% sure though this is the file I need to change. :(
<div class="base-image">
#if (! $ebook->book_cover->exists)
<div class="image-placeholder">
<i class="fa fa-picture-o"></i>
</div>
#else
<a class="base-image-inner" href="{{ $ebook->book_cover->path }}">
<img src="{{ $ebook->book_cover->path }}" alt="{{ $ebook->name }}">
</a>
#endif
</div>
I'm not sure about it but I think,
<a class="base-image-inner" href="{{ $ebook->book_cover->path }}">
In this section, see other available keys instead of using ->path, Not so sure but maybe you'll find another key
Do this:
You can use explode and then pop the result array
<?php
$string = "http://localhost/eBook/public/storage/http://mywebsitestorage.com/picture.png";
echo getImageNameFromString($string);
function getImageNameFromString($string) {
$explodeString = explode("/", $string);
return array_pop($explodeString);
}
output
picture.png

How to open a pdf file in new tab from controller PHP (laravel)

Hi I am new to php programming. I am using laravel 5.2 now.
I have used an external service to create a pdf from my html page. For that I have used pdfcrowd. I wrote the content to a pdf file in my local system. Its location is /var/www/html/testproject/test_pdf.pdf.
It works fine. Now my requirement is that, I have to open that pdf file in a new tab from my controller. My controller name is adminController. Inside that I used a function to open the pdf.
Please see the code below:
public function open_pdf() {
$file_location='/var/www/html/testproject/test_pdf.pdf';
----------?
}
Please suggest your ideas.
The important thing to understand is that, we can't open a new tab from php controller. So In the view page (testpage.blade.php) I used 'target="_blank" code in a tag. please see the code below:
<a href="{{ url('/') }}/testproject/openpdf"> target="_blank"
Try to add target="_blank" in your html tags it will work fine.
You may have use like this
<a href="{{ url('/') }}/testproject/openpdf">
please try with this
<a href="{{ url('/') }}/testproject/openpdf" target="_blank">

Laravel 4 Url Helper functions not matching protocol

I had read somewhere the the Laravel helper functions route() and url() would match the protocol of the current page. This has not been my experience and I cannot find a way to do it cleanly.
<!-- https://domain.com/public/route1 -->
<a href='{{ route('route2') }}'> domain.com/route2 </a>
404 due to lack of "/public/" and no https
<a href='{{ url('/route2') }}'> domain.com/public/route2 </a>
proper path but still drops https
<a href='{{ secure_url('/route2') }}> https://domain.com/public/route2 </a>
proper path and protocol but is explicit
Is there a way to do what I want? It seems absurd to me that these helpers would be unable to accomplish something this simple. Something that the following line of code does with no tricks (its downside being the obvious loss of benefits associated with using helper functions [UPDATE: in particular having everything break when running locally due to a different app path]):
<a href='/public/route2'> https://domain.com/public/route2 </a>
To further convolute things I would like to add that when running on localhost, the first version {{ route('route2') }} produces a path that includes /public/, though it is possible that I have a different config file somewhere on the server (this is a very large project). Bonus points if someone can point me in the right direction for that issue as well.
routes.php:
Route::group(array('prefix' => 'route2' ), function(){
Route::get('/', array(
'as' => 'route2',
function(){
return View::make('pages.route2');
}
));
...
});
Thank you kind souls!
UPDATE:
I have continued searching and been unable to find any relevant information regarding Helper function url generation. It seems the majority of info with similar keywords is about forcing HTTPS site-wide (server config) or setting up redirects. It just seems so much simpler to generate the correct link in the first place. Plain HTML can do it, so should Laravel.
UPDATE 2:
Using the asset helper seems to work how I would expect url to. Please someone tell me this hack is not my only option...

Laravel 5 check if current url matches action

I am currently generating my application urls using {{action('Namespace\Class#method')}}. How would I check if the current page request maps to that current action Namespace\Class#method?
I would like to do something like:
<a href="{{action('Namespace\Class#method')}}
#if (currentAction('Namespace\Class#method'))
class="active"
#endif
>Some link</a>
How would I achieve this in Laravel 5?
There's no built in method for this, however you can retrieve the current action name with Route::currentRouteAction(). Unfortunately this method will return a fully namespaced class name. So you will get something like:
App\Http\Controllers\FooBarController#method
You can either check for that or use something like ends_with so you don't have to specify the full path:
#if(ends_with(Route::currentRouteAction(), 'FooBarController#method'))
You might also consider naming your routes with 'as' => 'route.name'. This would allow you to use: Route::is('route.name')
Actualy I had it simplified to the following code:
<li class="#if(Route::is('getLogin')) active #endif">Login</li>
That assumes that you have named routes. Which is a good idea in the first place, because you migth change the url of an action without going through your whole project to change the links to that action.
I know this is old, but for what it is worth.
Laravel has a couple of built-in helper methods for referring to URLs action and route.
action
Your route file would look like this.
Route::get('/funtastic', 'FuntasticController#show');
Your blade view would look like this
<a href="{{action('FuntasticController#show')}}
#if(action('FuntasticController#show') == Request::url())
class="active"
#endif
>Some link</a>
route
If you use named routes.
<a href="{{route('namedRoute')}}
#if(route('namedRoute') == Request::url())
class="active"
#endif
>Some link</a>
This is how I do it without any named routes
<a class="{{ str_contains(request()->url(), '/some-page') ? 'active' : '' }}" href="/some-page">Some Page</a>
This is not a perfect solution but it works for most cases
It works perfectly for me.
<a class="#if (\Route::current()->getName() == 'device_media') active #endif" href="{{ route('device_media', ['brand_slug'=>$brand->slug, 'slug'=>$device->slug]) }}" > Media</a>

Categories