Closed paulocastellano closed 3 years ago
It looks like there are two small errors in the examples you shared, false
should be the third parameter to route()
, outside the brackets, and I think the route you want is portal.posts.show
, not posts.show
. Does this work?
- route('posts.show', {'subdomain': 'company-1', 'id': post.id, false})
+ route('portal.posts.show', {'subdomain': 'company-1', 'id': post.id }, false)
Can you also share a working example of using Laravel's route()
function in PHP to accomplish this? Thanks.
It looks like there are two small errors in the examples you shared,
false
should be the third parameter toroute()
, outside the brackets, and I think the route you want isportal.posts.show
, notposts.show
. Does this work?- route('posts.show', {'subdomain': 'company-1', 'id': post.id, false}) + route('portal.posts.show', {'subdomain': 'company-1', 'id': post.id }, false)
Can you also share a working example of using Laravel's
route()
function in PHP to accomplish this? Thanks.
Yes, sorry i wrong... the param false is out the brackets...
Helper:
<a :href="route('portal.posts.show', {'subdomain': team.subdomain, 'slug': post.board.slug, 'postSlug': post.slug}, false)" class="flex w-full">
Full URL:
http://feedback.domain.test/bugs-fixes/p/bug-on-integration?subdomain=feedback
Helper:
<a href="{{ route('portal.changelogs.show', ['subdomain' => $project->subdomain, 'slug' => $post->slug], false) }}" >
Full URL:
https://feedback.domain.test/changelogs/april-improvements
Some help links from laravel:
@bakerkretzmar If i can help, please tell me.
@paulocastellano to get the complete absolute URL, including the origin, route('portal.posts.show', { subdomain: 'portal-1', slug: 'posts', postSlug: 'my-first-post' })
works for me. To get the relative URL without the domain, you can just omit the subdomain
parameter: route('portal.posts.show', { slug: 'posts', postSlug: 'my-first-post' }, false)
works for me and returns /posts/p/my-first-post
. Can you try that?
@paulocastellano to get the complete absolute URL, including the origin,
route('portal.posts.show', { subdomain: 'portal-1', slug: 'posts', postSlug: 'my-first-post' })
works for me. To get the relative URL without the domain, you can just omit thesubdomain
parameter:route('portal.posts.show', { slug: 'posts', postSlug: 'my-first-post' }, false)
works for me and returns/posts/p/my-first-post
. Can you try that?
Yeah this will work, but my problem is, my application run with subdomain and custom domain ex:
subdomain.my-application.com subdomain.client-domain.com
subdomain.client-domain.com are proxy to subdomain.my-application.com.
And some customers can have or do not have custom domains.
Because of this, I need every pass subdomain param, because the application is based on a subdomain.
because with subdomain param, I will get all information of the customer.
Ahh, gotcha. That might not be possible in Ziggy directly, I think you'll have to work around it.
The issue is that your route definition, Route::group(['domain' => '{subdomain}.' . parse_url(env('APP_URL'), PHP_URL_HOST)], function () { ... })
, uses the APP_URL
environment variable, which is always https://my-application.com
. Ziggy will usually use url('/')
instead, which will always be the URL of the current request, but because your route actually contains the domain in the route definition itself, Ziggy will always have it in the config of every route as the domain
property, hard-coded to APP_URL
.
You'll have to overwrite that part of the route client-side with something like this:
import route from 'ziggy';
const customRoute = (name, params, absolute, config) => {
return window.location.origin + route(name, params, false, config);
}
I didn't run that code so you may need to tweak it, but that should replace the URL origin with the current one in the browser, so it'll always match whatever the user sees / is using. Does that make sense?
Ahh, gotcha. That might not be possible in Ziggy directly, I think you'll have to work around it.
The issue is that your route definition,
Route::group(['domain' => '{subdomain}.' . parse_url(env('APP_URL'), PHP_URL_HOST)], function () { ... })
, uses theAPP_URL
environment variable, which is alwayshttps://my-application.com
. Ziggy will usually useurl('/')
instead, which will always be the URL of the current request, but because your route actually contains the domain in the route definition itself, Ziggy will always have it in the config of every route as thedomain
property, hard-coded toAPP_URL
.You'll have to overwrite that part of the route client-side with something like this:
import route from 'ziggy'; const customRoute = (name, params, absolute, config) => { return window.location.origin + route(name, params, false, config); }
I didn't run that code so you may need to tweak it, but that should replace the URL origin with the current one in the browser, so it'll always match whatever the user sees / is using. Does that make sense?
I trying create an solution this to fix my problem.
I fixed for GET routes, but POST routes not join inside route method?
example:
const customRoute = (name, params, absolute, config) => {
//ROUTE POST NOT JOIN HERE
}
It's correct?
Sorry I'm not sure what you mean by "join", are you using the @routes
Blade directive?
If you are using @routes
, try something like this in your HTML right after @routes
:
<script>
const customRoute = (name, params, absolute, config = Ziggy) => {
return window.location.origin + route(name, params, false, config);
}
</script>
Otherwise, if you're setting up Ziggy in your JavaScript code, make sure you import the route definitions and pass them to the custom route function:
import route from 'ziggy';
import Ziggy from './ziggy.js';
const customRoute = (name, params, absolute, config = Ziggy) => {
return window.location.origin + route(name, params, false, config);
}
Does that help?
@bakerkretzmar thanks for your help!
I found the "solution" to my problem, after try a lot, the solution to my case was:
app.js
.mixin({
methods: {
route: (name, params, absolute) => {
let isAbsolute = true;
if (name) {
if (name.includes("portal.")) { // all my public routes, the name start with portal.
isAbsolute = false;
params.subdomain = null;
}
}
return route(name, params, isAbsolute, {
...Ziggy,
});
},
},
})
But this works only for GET ROUTES, for POST, PUT and DELETE routes i still change manually every component removing de subdomain from param (absolute routes not need subdomain), example:
login() {
this.loginForm.post(route("portal.auth.login", {}, false), {
preserveScroll: true,
preserveState: true,
onSuccess: () => (
(this.isOpen = false), this.loginForm.reset()
),
});
},
I hope this "solution" can help other people in the feature.
@bakerkretzmar you want close this issue?
Ziggy version
1.3.5
Laravel version
8.40
Description
Hi,
I have a wildcard application based on subdomain.
Ex: company-1.domain.com this works well.
The problem is when this "company-1" want use a custom domain via proxy, ex: portal.company-1.com and I need use absolute path.
Because ziggy not understand what param is from subdomain and what param is for URL.
The route helper:
Show: /posts/ID?subdomain=company-1 instead of show /posts/ID (laravel route helper works well with this method)
Ziggy call and context
Ziggy configuration
Route definition