Varun Sharma
Varun Sharma

Reputation: 1

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.

Upvotes: 0

Views: 140

Answers (4)

Maik Lowrey
Maik Lowrey

Reputation: 17556

A root-relative URL starts allway with a / character, to look something like <a href="/cars/create">create car</a>.

The link you posted: <a href="cars/create">create car</a> 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');

Upvotes: 0

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

<a href="{{route('yournamelink')}}">goto page</a>

Upvotes: 0

Dmitriy Kovalev
Dmitriy Kovalev

Reputation: 11

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.

Upvotes: 0

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

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.

Upvotes: 1

Related Questions