-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem using livewire #58
Comments
Hi, It's really hard to tell what's going on. |
Not sure if this is the problem, but I'm also experiencing some difficulties. Livewire makes under the hood requests to the backend, to a dedicated |
I have the same issue. After an update, {{ \Route::localizedUrl('nl') }} results in "http://site.test/livewire/message/slideshow" (where slideshow is the name of the Livewire component). I hope this gets fixed! Seems this is a known issue with Livewire, and a number of packages exist to deal with it, such as this one. I guess it would be nice if Localized Routes was aware of Livewire, or could be made aware of it in a way. |
Hello, It seems you can configure the middleware in the Livewire config. If you add the You can not use the option Some additional findings... The The part that comes before that is the Unfortunately, you can not detect the locale in a config file. |
Hi, Sorry for the late followup. I created a fresh Laravel test app with this package and Livewire included. Locale ProblemFor some reason, Livewire's locale is always set to the last locale in my Initial page load: When I click the button to update the Component: Livewire requests should go through the Current Route / Request ProblemAs mentioned before, Livewire makes ajax calls to a I was able to fix this locally, but I don't know if this is a legit way. First I need to inject the request (this is not on GitHub yet): And then I inject a recreated version of the original request when we're in a Livewire requests, using a middleware. public function handle(Request $request, Closure $next): Response
{
if ($this->isLivewireRequest()) {
$sessionRequest = Request::create(session('current-url'));
$sessionRequest->setRouteResolver(function () use ($sessionRequest) {
return Collection::make(Route::getRoutes())->first(function ($route) use ($sessionRequest) {
return $route->matches($sessionRequest);
});
});
app()->bind(LocalizedUrlGenerator::class, function () use ($sessionRequest) {
return new LocalizedUrlGenerator($sessionRequest);
});
} else {
session()->put('current-url', $request->fullUrl());
}
return $next($request);
}
protected function isLivewireRequest(): bool
{
return class_exists(\Livewire\LivewireManager::class)
&& app(\Livewire\LivewireManager::class)->isLivewireRequest();
} Now the macro works event when the Component gets updated: I would need to do this for every class that uses the current route or request. If anyone has a better understanding of how this all works, I'd love to hear it. |
Hi @ivanvermeyen thanks for the comprehensive writeup. I've since written a temporary workaround that just works ™️. By overriding the packages UrlGenerator, detecting a Livewire request & grabbing the correct locale from the request referrer. This is hacky as hell though, so I'd love to see a more robust solution. I vaguely remember Caleb (Livewire's creator) talking about supporting dynamic url's for Livewire update requests in V3, which will release soon (hopefully). It might be easier to integrate with in V3. |
The problem seems to be caused by temporarily updating the App locale when generating the URLs for alternative locales. laravel-localized-routes/src/Illuminate/Routing/UrlGenerator.php Lines 30 to 40 in 2765567
If I comment out the 2 calls to Will try to find out why this issue is occurring. |
Finally discovered the source of all evil. In the code of the try {
return URL::route($this->route->getName(), $parameters, $absolute, $locale);
} catch (InvalidArgumentException $e) {
return '';
} By catching the exception, the locale that was temporarily changed in the underlying if ($locale !== null && $locale !== $currentLocale) {
App::setLocale($locale);
}
try {
$url = parent::route($resolvedName, $parameters, $absolute);
} finally {
if ($locale !== null && $locale !== $currentLocale) {
App::setLocale($currentLocale);
}
} I'll fix his first and then figure out how to handle the Request issues. |
Not sure if this helps, but you might need to add a persistent middleware to capture the locale url param https://laravel-livewire.com/docs/2.x/security#persistent-middleware Though it's been a while since first encountering this. If you need an extra set of eyes I'm happy to pair on it |
@gwleuverink Thanks! I also came across the persistent middleware. |
After a lot of thinking and tinkering, I've come up with a working prototype that handles the Livewire requests always being the ajax endpoint instead of the expected original URL. I could run my current test suite on it by using the current URL for now, instead of the I pushed a few changes beforehand, to inject the In the You can see in the middleware that I had to jump through some hoops to make everything work. https://github.com/codezero-be/laravel-localized-routes/pull/93/files If @gwleuverink or anyone would like to review, I'm open for any feedback. https://github.com/codezero-be/laravel-localized-routes/tree/support-livewire |
Hello, Ive problems using livewire. All works fine, but when I type the input, data refresh and dissapear data from views/livewire/filtro.
When I click on url, data comes well.
Ive to refresh url to view data.
Ive tried displya blade without Front Controller, clear cache, clear routes
Http/Livewire/Filtro
`use Livewire\WithPagination;
class Filtro extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
protected $queryString = ['marca'];
public $marca;
}`
routes/web
`<?php
use Illuminate\Support\Facades\Route;
*/
// Localized
Route::localized(function () {
});
Route::get('/', function () {
return view('front.index');
});
Route::get('/index', [App\Http\Controllers\FrontController::class, 'index'])->name('front.index');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class)
->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);`
views/livewire/filtro
`
@else
No hay productos
@endif
{{ $productos->links() }}
App/Http/Comtrollers/FrontController
`<?php
namespace App\Http\Controllers;
use App\Models\Front;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Producto;
use App\Models\Link;
use Illuminate\Support\Facades\Session;
use App;
class FrontController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
}
`
The text was updated successfully, but these errors were encountered: