sakurikii's avatar

How to get route prefix of a specific route by name?

I know you can get route prefix of current route like this

request()->route()->getPrefix()

but what if I want to get the prefix of a route with name tags.show, some thing like(not worked obviously)

route('tags.show')->getPrefix()

The part http://192.168.100.150/tags is currently hardcoded, I want it dynamic in case of change route/domain in the future

Image

Thanks!

0 likes
7 replies
sr57's avatar

If for only a specific route you can change the url in the route definition (web.php), it's the usage case of route names.

1 like
sakurikii's avatar

I think you misread my question, I'm asking instead of modifier the string http://192.168.100.150/tags everytimes I change the url in the web.php. Can I get it's prefix by route name(which will never change)? Laravel only support get prefix of current working route, not some random route.

sr57's avatar

Ok !

2 points

to change it to http://my_site

just add it in your /etc/hosts (have a look on the web if you don't know how)

For non local IP? you can do the same way for local test, or define your DNS in your provider settings.

  • /tags is part of your Laravel app, and it's not abnormal that's it's hardcoded, so change in the app iis the standadr way. That said if you want to have 'synonym' you can use redirection (with Laravel or with your web server). See redirection in the doc or o the web to have more info.
1 like
sakurikii's avatar

English isn't my first language, so sorry if my question confused you.

Say I have a route like this on web.php

Route::get('/tags/{tag}', TagController::class)->name('tags.show');

I'm working on a local dev so my domain would be 192.168.100.150(my local IP) or locahost, I have a blade file that show the admin tag create

<div class="col-span-3 sm:col-span-2">
    <label for="slug" class="block text-sm font-medium text-gray-700">
        Slug
    </label>
    <div class="mt-1 flex flex-col rounded-md shadow-sm">
        <div class="flex flex-wrap">
            <span
                class="inline-flex items-center px-3 rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-gray-500 text-sm">
                http://192.168.150.100/tags/
            </span>
            <input wire:model="slug" type="text" name="slug"
                class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-r-md sm:text-sm border-gray-300"
                placeholder="tag-name">
        </div>
        @error('slug')
        <p class="text-red-500 mt-1">{{ $message }}</p>
        @enderror
    </div>
</div>

The part http://192.168.150.100/tags/ act like a preview for the url of what the tag might look like

Now, if I change my route form /tags to something like /the (tag in vietnamese)

Route::get('/the/{tag}', TagController::class)->name('tags.show');

I need to open the blade file above and changehttp://192.168.150.100/tags/ to http://192.168.150.100/the/,

or if I deploy my project to internet on a domain name like https://my-project.com. I need to change it to https://my-project.com/tags

Isn't a very efficient way ain't it?

Anyway, I don't think there is a way to do it so I'm just gonna hardcoded it for now. Thank for answered!

MichalOravec's avatar
Level 75

@sakurikii Just use named route, I don't know why is it a problem for you.

<span class="inline-flex items-center px-3 rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-gray-500 text-sm">
    {{ route('tags.show', ['tag' => null]) }}
</span>
1 like
sakurikii's avatar

Thanks! I don't know you can make route param optional and just pass null in so I never thought about trying it

sr57's avatar

English isn't my first language,

me too, and for many of us ...

But we have one common language, it's code ... next time put code directly it will easier to make us understand.

Too easy to @michaloravec with code :-)

1 like

Please or to participate in this conversation.