Dominate vue router with Laravel router - laravel

I am building an application with Laravel and Vue.js. I am using vue router. With this I am controlling all routes. Now, I want to make an url xyz.com/admin which will different (Laravel new route, I want to work with this admin separately). I am using this below code, but not working.
Route::get(
'/admin/{view?}',
"AdminController#index"
);
Route::view('/{any}', 'home')->where('any', '.*');
I also remove the admin link from <router-link>
<v-list-tile-title v-if="isAdminMethod===true">
Admin Panel
</v-list-tile-title>
<v-list-tile-title v-else>
<router-link :to="{ path: i.to }">
{{ i.title}}
</router-link>
</v-list-tile-title>

How about you try something like
Route::get(
'/admin/{view?}',
"AdminController#index"
);
or it might be worth looking into something like InertiaJS.
Let me know if you have any further queries.

Related

Why does it show me that the "Route [searchbybrand] not defined." in a laravel project?

Im working on a e-commerce web-app using laravel. I have a section called shop by brand with the pictures of the brands and when i click on the picture i want it to show me the products of that brand.
This is the blade part of my code where i call the route:
<div class="brand-slides owl-carousel owl-theme">
<div class="brand-item">
<img src="assets/img/brand/shopbybrand1.jpg" alt="image">
</div>
</div>
This is my controller:
public function searchVendor()
{
$searchproduct="Fanola";
$product=product::Where('vendor','LIKE',"%$searchproduct%")->get();
return view('home.userpage',compact('product'));
}
At the moment its just a simple code that will work with the "Fanola" brand only. I did it this way just to test if it works.
And this is my route in my web.php:
Route::get('/searchbybrand', [HomeController::class, 'searchVendor']);
The error it shows me when i serve it is "Route [searchbybrand] not defined."
Ive tried clearing the route cache and it still doesnt work. I asked chat gpt about this and it gave me different ideas like defining the route like this:
Route::get('search-by-brand', 'HomeController#searchVendor')->name('searchbybrand');
When i do it like this it shows a different error: "Target class [HomeController] does not exist."
I also tried composer dump-autoload, php artisan config:clear and php artisan cache:clear as per its suggestions but none of them worked.
this is because your route doesn't have a name you can fix it like this
Route::get('/searchbybrand', [HomeController::class, 'searchVendor'])->name('searchbybrand');
or instead of that in your blade you can use url instead of route
<div class="brand-slides owl-carousel owl-theme">
<div class="brand-item">
<img src="assets/img/brand/shopbybrand1.jpg" alt="image">
</div>
</div>
and for your second problem which says Target class [HomeController] does not exist. add this in top of your web.php
use App\Http\Controllers\HomeController;
also at the end run
php artisan optimize

Larave Pagination is showing localhost not my domain

I am using a laravel application where all my pagination URLs are wrong. They are not using my domain like https://example.com instead it is showing localhost:3000 on the live server.
All other URLs and routes are working fine but pagination has issues. Please guide, I have used
{-- $order->link() --}
{{ $orders->render() }}
also, I have tried
{!! str_replace(request()->server('SERVER_ADDR'), "example.com", $orders->links()) !!}
But it always shows localhost:3000

How to integrate Spatie's laravel-permission with JetStream properly?

I have a nicely working basic install of Laravel JetStream and Spatie's laravel-permission in Laravel 8.
I can assign a role to the user during registration via
$user->assignRole('visitor');
return $user;
and can restrict the available menu items on the user's dashboard through the permissions I have assigned to the role in my seeder filés run method:
Permission::create(['name' => 'access profile']);
Permission::create(['name' => 'access logout']);
$visitor = Role::create(['name' => 'visitor']);
$visitor->givePermissionTo('access profile');
and through the can directive in the view, like:
#can('access profile')
<!-- Account Management -->
<div class="block px-4 py-2 text-xs text-gray-400">
{{ __('Manage Account') }}
</div>
<x-jet-dropdown-link href="{{ route('profile.show') }}">
{{ __('Profile') }}
</x-jet-dropdown-link>
#endcan
So by that, I can hide the menu item as per role but unfortunately, I can still access the functionality directly, by knowing the exact URL.
I guess I have to write a middleware to restrict access to certain functions, but how exactly?
What is the proper and accepted way to handle this problem in this stack?
Thanks!
Armand
So everything seems fine BUT (!)
How is it possible to forbid direct access to the hidden items? I guess in this case routes are controlled by sanctum, while roles and permissions are by Spatie's package.
Is it possible to link the two?
Thanks!
Did you try this? It seems like they added exactly the same for Spatie. Nevertheless I think you need to add a gate permission check like
abort_if(Gate::denies('permission'), Response::HTTP_FORBIDDEN, '403 Forbidden');
on every action
I would see if you can utilize laravel's built in can middleware. Then you might be able to update your route definitions. Something like
Route::get('/profile', 'ProfileController#index')->middleware('can:access profile');
I haven't done this with the package you're using, but I think it should work if the other built-in functionality like blade #can work.

Laravel API route - exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

I'm making a Blog for school on my portfolio website, now I'm doing this in VueJS and Laravel and for this I need API routes.
Now I want to delete a comment with a specific ID but when I push the delete button it gives the error:
exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
The error is in the {routeCommentID} part of the next route:
Route::post('/deleteComment/{routeCommentID}', 'CommentController#delete');
What did I do wrong? Because when I remove that it works fine, but I need this part because I have to remove a comment with a specific ID.
Run php artisan route:list and check if route like '/deleteComment/{routeCommentID}' exists and whether you use that route in your Vue application.
for deleting a post it's better to use
Route::delete('/deleteComment/{id}', 'CommentController#delete');
and checkout your blade for deletion
it should be something like below
<form action={{ 'wanted route' }} method="post">
#csrf
#method('delete')
// your code
</form>

Laravel: how to connect controller to view without using or defining a route

I'm trying to find the route that has connected a view and controller in a code i'm editing.
This is how route is included in the view.
<a href="{{ route('admin.provider.edit', $provider->id) }}" ></i> Edit</a>
Try
use \App\Http\Controllers\ControllerName;
{{ ControllerName::Functionname($params); }}
Go to your console a run the next command:
php artisan route:list
This will output something like this:
Right there you can see a lot of information such as: Domain, Method, URI, Name, Action and Middleware. That's the best way to find what you want!

Resources