Why am I getting 404 error on my livewire.js connection? - laravel

I am using Laravel 8. "wire:model" also not working because of this.

you need to setup livewire base url
in config/livewire.php
'asset_url' => env('APP_URL', 'http://localhost'),,
then in .env
APP_URL=your_app_url

in config/livewire.php
'asset_url' => url('/'),

You need to setup the livewire app url in config/livewire.php.
'app_url' => url('/'),

Just the following code to your admin or control panel routes:
use Livewire\Controllers\HttpConnectionHandler;
Route::post('livewire/message/{name}', [HttpConnectionHandler::class, '__invoke']);
And remember to override the url using this one linear:
<script>
window.livewire_app_url = '{{route('admin.index')}}';
</script>
More detailed answer could be found here
Laravel - Livewire, how to customize the global message URL?

Related

404 Not Found on sanctum/csrf-cookie path

So I've been building a projet based on laravel. I'm building on a SPA foundation with sanctum as my authorization package. It works perfectly. Then I deploy the project to the server and everytime I try to login there is a 404 error on /sanctum/csrf-cookie.
How could this happen? Is it because the SanctumServiceProvider not working.
The problem is when you define sanctum prefix, the route become something else like this:
you can check your routes with : php artisan route:list
as you can see the /sanctum/ is removed and when you check the route /sanctum/csrf-cookie it will not be and throws 404 error. So you have two options:
add this prefix: 'prefix' => 'api/v1/sanctum'
or
change GET call to api/csrf-cookie
You need to check if you're setting correct axios defaults for your /sanctum/csrf-cookie api call.
You can set it as follows
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost"; //Before sending get request
axios.get("/sanctum/csrf-cookie").then(async () => {
//Logic to handle login
});
If defaults are not set properly url becomes http::localhost::8080/sanctum/crf-cookie which is where frontend is serving but instead it has to be http::localhost/sanctum/csrf-cookie
Note: localhost example is just for explanation. For production server make sure your url is correct and api call is on backend.
I solved this issue by adding:
AllowOverride All
to the apache directory config
add in last line inside config/sanctum.php
'routes' => false,
Add in config/cors.php
'paths' => ['*']

Laravel sanctum change csrf cookie route

How could I change laravel sanctum csrf cookie route to /api/sanctum/csrf-cookie ?
I tried adding this to api.php routes:
use Laravel\Sanctum\Http\Controllers\CsrfCookieController;
Route::get('/sanctum/csrf-cookie', CsrfCookieController::class . '#show')->middleware('web');
But it looks for this controller under app/http/controllers where it doesn't exist.
So if anyone wondering, there should be prefix within config file that is default set to 'sanctum' within a package service provider.
So if you want to change it to API routes you should go to config/sanctum.php and add 'prefix' => 'api'.
you can create a controller CsrfController and make it extends
(Laravel\Sanctum\Http\Controllers\CsrfCookieController)
use Laravel\Sanctum\Http\Controllers\CsrfCookieController
class CsrfController extends CsrfCookieController {}
and then you can link your route
Route::get('/sanctum/csrf-cookie', 'CsrfController#show')->middleware('web');
another flexible solution is to set
'routes' => false
in config/sanctum.php ,then define a new route in routes\api.php
use Laravel\Sanctum\Http\Controllers\CsrfCookieController;
Route::get('/csrf', [CsrfCookieController::class, 'get'])->name('csrf');
like so, you can choose the proper route to suit your needs

Laravel 5.5 Route groups

I was having this in my website using Laravel 5.3 :
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware'=>'auth'], function(){
Route::resource('posts', 'PostsController');
});
This lets me go to the admin panel using: mywebsite/public/admin/posts.
Now, when I migrated the site to Laravel5.5 I got this error Route[admin.posts.create] not defined when i attempt to open the link Create post which was working fine before.
I know that routing system has changed but I did not know how to have such links in new Laravel5.5. I tried url instead of route but I got the same error. I also checked the new documentation but I did not get exactly how to have the same link system.
Can anyone have a better explanation of this new routing system? (I have to migrate the site to 5.5).
Laravel names resource routes by default, you can check them by running php artisan route:list
If you want to override them for any reason you can pass in an array when you define the route and override each individual route name like so:
Route::resource('posts', 'PostsController', ['names' => [
'create' => 'admin.posts.build'
]]);

How to use Laravel Debugbar in controller?

I got an error Class 'App\Http\Controllers\Debugbar' not found after trying to use in controller action Debugbar::error('Error!');
LARAVEL DEBUGBAR WAS INSTALED IN THIS WAY:
in console composer require barryvdh/laravel-debugbar
in config/app.php adding Barryvdh\Debugbar\ServiceProvider::class and 'Debugbar' => Barryvdh\Debugbar\Facade::class,
Laravel 5.3
Debugbar is working in the website - I can see it at the bottom even after this error. But how to fix this critical error message?
Either explicitly use the Facade or use Debugbar from the root namespace.
\Debugbar::error('hi');
// OR
use \Debugbar;
Debugbar::error('hi');
// OR
use Barryvdh\Debugbar\Facade as Debugbar;
Debugbar::error('hi');

How to route to a controller method in Laravel Moduler development?

I am using "artem-schander/l5-modular": "dev-master" for laravel moduler development.
For example i create an Admin module.
Folder structure is App/Modules/Admin.
So controller related to Admin modules placed under App/Modules/Admin/Controllers/ directory.
All routes related to Admin module are placed in App/Modules/Admin/routes.php file.
Here how it looks
Route::group(array('module' => 'Admin', 'middleware' => ['web'],'namespace' => 'App\Modules\Admin\Controllers'), function() {
Route::resource('admin', 'AdminController');
});
All view files related to admin module placed in App/Modules/Admin/Views folder.
I am trying to access Admin's index view using this route
Route::get('/', 'AdminController#index');
This route is place in laravel default routes.php file.
and when i browse ,I am getting this error
Class App\Http\Controllers\AdminController does not exist
From this i understood , laravel looking AdminController in its default path.
How can i overcome this challenge ?
You can access a controller by full qualified namespace if it is not in default path.
Try:
Route::resource('admin', '\App\Modules\Admin\Controllers\AdminController');
I have found two way to do it.
First Option
Changing the $namespace in RouteServiceProvider.php .
For me
private $namespace='\App\Modules';
So for Admin module i can use route as
Route::get('/', 'Admin\Controllers\AdminController#index');
I think this is bad idea to change Laravel's default value.
Second Option
Providing full path of the Controller.
So route would be like this
Route::get('/','\App\Modules\Admin\Controllers\AdminController#index');

Resources