Why I am getting wrong URI? - php

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');

Related

Redirect from a file in Laravel 8

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.

how to make working menu tab with laravel 5.4

I am working on Laravel 5.4
I have created a menu, in which three tabs Home, about and contact. When I click on Home then it should be on home page. When click on about, it should be on about page....
web.php:
<?php
Route::get('/', function()
{
return View('method1.home');
});
Route::get('about', function()
{
return View('method1.about');
});
**method1 is folder in resources\views**
home.blade.php:
#extends('method1.dashboard')
#section('content')
<h1>This is home page</h1>
#endsection
about.blade.php is:
#extends('method1.dashboard')
#section('content')
<h1>This is about page</h1>
#endsection
dashboard.blade.php is:
#include('method1.includes.menu-header')
menu-header.blade.php is:
<li class="active"> Home</li>
<li> About</li>
But when I click on home or about page. It shows Page is not found.
My laravel projet folder name is admin_laravel. When I run http://localhost/admin_laravel/ then it shows home page and when run http://localhost/admin_laravel/about it shows about page.
But when I click on about menu button then in browser shows link http://localhost/about. Means it is not going with http://localhost/admin_laravel/about and page is not showing.
You are missing something there, For a quick fix you can do this:
<li class="active"> Home</li>
<li> About</li>
You can also give route name and go with route method:
Route::get('about', function()
{
return View('method1.about');
})->name('about');
Then:
<li> About</li>
Here is the details: https://laravel.com/docs/5.2/helpers#method-route
You're hard-coding your URLs. When you have <li>About</li> you're telling your browser to go to the about path from the root of the domain (which is what happens when you prefix your URL with /), which in this case is http://localhost/.
There's a few things you should be doing. First, set the base URL for your project, you can update APP_URL in your .env file
APP_URL=http://localhost/admin_laravel
or the url option in config/app.php.
'url' => env('APP_URL', 'http://localhost/admin_laravel'),
Secondly, when generating URLs in Laravel there are quite a few options. If you're not using named routes, then you should use the url helper method to generate your URLs:
<li>About</li>
That will make sure that your URL is based at the root of your project, rather than the root of the domain. When you use url in conjunction with the correct setting as described above, your URLs will be generated correctly.

how to call a blade file dynamically in laravel?

I'm calling a blade that way:
<a href="{{ asset('views/quem-somos.blade.php') }}" class="dropdown-toggle"
data-toggle="dropdown">Quem somos</a>
but is returning that in the file does not exist, how would the correct way of calling in href be?
You shouldn't be calling the view files like that, create a url in the routes.php/web.php depending on your laravel version and have the url point to the blade instead.
Route::get("/quem-somos", function() {
return view("quem-somos");
});
And in the current file, change the link to
Quem somos
Use #include('quem-somos') wherever you're wanting the blade to be inserted.

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