Laravel Route Can Not Find By Route Name - laravel

This is My Route
Route::namespace("Core\Controller\Site")
->prefix("/")
->name("site.")
->group(function () {
Route::get("{slug}", "NewsController#index")->name("news.index");
Route::get("show/{slug}", "NewsController#show")->name("news.show");
Route::get("{slug}", "ArticleController#index")->name("article.index");
Route::get("show/{slug}", "ArticleController#show")->name("article.show");
Route::get("{slug}", "VideoController#index")->name("video.index");
});
This is My a href which is I used This Route
It gives Such Kind Of Error
Route [site.news.show] not defined.

You are using the same route paths (show/{slug} and {slug}) with the same methods for different route names.
I think they override each other. Try to use different route paths.

change the name of the route and it will work.
show_news and show_article
Route::namespace("Core\Controller\Site")
->prefix("/")
->name("site.")
->group(function () {
Route::get("{slug}", "NewsController#index")->name("news.index");
Route::get("show_news/{slug}", "NewsController#show")->name("news.show");
Route::get("{slug}", "ArticleController#index")->name("article.index");
Route::get("show_article/{slug}", "ArticleController#show")->name("article.show");
Route::get("{slug}", "VideoController#index")->name("video.index");
});

Related

Add username as prefix in route URI in Laravel 9

I am building an application where users are registered and want to be redirected to their individual dashboard like this .
http://localhost/project/{username}/dashboard,
Now, its happening like
localhost/project/vendors/dashboard (here all users are accessing same URL)
but I want to make it like :
http://localhost/project/{username1}/dashboard, http://localhost/project/{username2}/dashboard
Googled lot but none of them are explained well and working.
Please assist with complete flow.
I want to declare the value of {username} globally and use it in route as prefix.
I dont want to use it before each name route. will use it as prefix and group with all vendors routes
I have made this, and its working as
localhost/project/vendors/dashboard
Route::prefix('vendors')->group(function () { Route::middleware(['auth:vendor'])->group(function () { Route::get('/dashboard', [VendorController::class, 'dashboard'])->name('vendor.dashboard'); });
});
You can specify route parameters in brackets like so {parameter}, change your code into this.
Route::get('project/{username}/dashboard', [UserDashboardController::class, 'dashboard'])
->name('user.dashboard');
In your controller you could access it like this.
class UserDashboardController
{
public function dashboard(string $username)
{
User::where('username', $username)->firstOrFail();
// something else
}
}
Seems like in your routes your are mixing vendor prefix logic in with something that in your specifications of what your urls should look like does not match. Which i think is making up some of the confusion on this case.
You can use route prefix like this
Route::prefix('{username}')->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
});
});

Route is not defined Inertia.js

In my controller, I have a method to redirect to a route:
return Redirect::route('/management');
This is my routes/web.php:
Route::get('/management', function () {
return Inertia::render('Management');
});
However, it throws an error saying Route [management] not defined.
The Redirect::route() method expects a named route, which you have not defined in your routes/web.php file.
In order to use a named route, you need to change your route to:
Route::get('/management', function () {
return Inertia::render('Management');
})->name('management'); // added
After that, you can redirect to named routes in Inertia like so:
return Redirect::route('management');
Since we're using named routes, and not URLs, you should not include the leading / in your route() call.

Laravel Routing - 404

I tried different routes but I am getting 404 on show,edit,store.
Auth::routes();
Route::get('/', 'IndexController#index')->name('index')->middleware('user');
Route::get('/user/profile', 'HomeController#index')->name('user')->middleware('user');
Route::get('/{product}/show', 'IndexController#show')->name('product')->middleware('user');
Route::prefix('cart')->group(function () {
Route::get('/', 'IndexController#cart')->name('cart')->middleware('auth');
Route::get('/{product}/add', 'IndexController#cartAdd')->name('cartAdd')->middleware('auth');
});
Route::prefix('dashboard')->group(function () {
Route::get('', 'AdminController#index')->name('admin')->middleware('admin');
Route::get('products', 'ProductController#index')->name('productIndex')->middleware('admin');
Route::get('products/create', 'ProductController#create')->name('productCreate')->middleware('admin');
Route::get('products/{product}', 'ProductController#show')->name('productShow')->middleware('admin');
Route::put('products/{product}', 'ProductController#update')->name('productUpdate')->middleware('admin');
Route::post('products', 'ProductController#store')->name('productStore')->middleware('admin');
Route::get('products/{product}/edit', 'ProductController#edit')->name('productEdit')->middleware('admin');
Route::post('products/{product}', 'ProductController#destroy')->name('productDestroy')->middleware('admin');
});
I'm trying to fix the routes by rearranging but didn't have any luck so far.
Route::get('/{product}/show', 'IndexController#show')->name('product')->middleware('user');
Move this route at the end. Using a dynamic value that soon in a route without any prefix is not the best way to go by the way. Maybe you can prefix it with something static.
Run php artisan optimize before testing it, to reconfig cache and routes.
It seems like it is because of the route key name here
public function getRouteKeyName()
{
return 'prod_name';
}
after deleting it all routes are working again.

How can I implement Resource Controllers if I use many "get" on the laravel?

I have routes laravel like this :
Route::prefix('member')->middleware('auth')->group(function(){
Route::prefix('purchase')->group(function(){
Route::get('/', 'Member\PurchaseController#index')->name('member.purchase.index');
Route::get('order', 'Member\PurchaseController#order')->name('member.purchase.order');
Route::get('transaction', 'Member\PurchaseController#transaction')->name('member.purchase.transaction');
});
});
My controller like this :
<?php
...
class PurchaseController extends Controller
{
...
public function index()
{
...
}
public function order()
{
...
}
public function transaction()
{
...
}
}
I want to change it to Resource Controllers(https://laravel.com/docs/5.6/controllers#resource-controllers)
So I only use 1 routes
From my case, my routes to be like this :
Route::prefix('member')->middleware('auth')->group(function(){
Route::resource('purchase', 'Member\PurchaseController');
});
If I using resouce controller, I only can data in the index method or show method
How can I get data in order method and transaction method?
You could try like this, Just put your resource controller custom method up resource route.
Route::prefix('member')->middleware('auth')->group(function(){
Route::get('order', 'Member\PurchaseController#order')->name('member.purchase.order');
Route::get('transaction', 'Member\PurchaseController#transaction')->name('member.purchase.transaction')
Route::resource('purchase', 'Member\PurchaseController');
});
For the resource controller, it is pre-defined by the Laravel, which contain only the 7 method.
Shown at below table.
So, if you want any other method, you have to definde by youself.
php artisan route:list
You can use this to check all the route you defined.
The other answers on here are pretty much correct.
From my other answer you linked this question in from, here that way based on what MD Iyasin Arafat has suggested, if you are using laravel 5.5+:
# Group all routes requiring middleware auth, thus declared only once
Route::middleware('auth')->group(function(){
# Suffix rules in group for prefix,namespace & name with "member"
Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
Route::get('purchase/order', 'PurchaseController#order')->name('purchase.order');
Route::get('purchase/transaction', 'PurchaseController#transaction')->name('purchase.transaction');
Route::resource('purchase', 'PurchaseController');
});
});
Grouping Methods ( ->group() ) :
Controller Namespace ( ->namespace('Member') )
Prepends to 'PurchaseController' to give
'Member\PurchaseController'
Route Name (->name('member.'))
Prepends to name('purchase.order') to give
route('member.purchase.order')
URI Request (->prefix('member'))
Prepends to /purchase to give example.com/member/purchase
As you can see, using the methods above with group() reduces repetition of prefix declarations.
Hint
Custom routes must always be declared before a resource never after!
Example to use if you have a lot of custom routes for Purchase Controller and how a second controller looks for member group:
# Group all routes requiring middleware auth, thus declared only once
Route::middleware('auth')->group(function(){
# Suffix rules in group for prefix,namespace & name with "member"
Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
Route::prefix('purchase')->name('purchase.')->group(function() {
Route::get('order', 'PurchaseController#order')->name('order');
Route::get('transaction', 'PurchaseController#transaction')->name('transaction');
Route::get('history', 'PurchaseController#history')->name('history');
Route::get('returns', 'PurchaseController#returns')->name('returns');
Route::get('status', 'PurchaseController#status')->name('status');
Route::resource('/', 'PurchaseController');
});
Route::prefix('account')->name('account.')->group(function() {
Route::get('notifications', 'AccountController#notifications')->name('notifications');
Route::resource('/', 'AccountController');
});
});
});

Redirect to a specific domain specified route

Consider the following route set up:
Route::group(['domain' => 'blog.adambalan.local'], function() {
Route::get('login', 'BlogController#login');
Route::get('blogs', 'BlogController#getBlogs');
Route::post('postLogin', 'BlogController#postLogin');
});
Now consider the following, which is in the postLogin:
if (Auth::attempt($credentials)) {
Session::flash('success', "Welcome back Adam. Care to manage your blogs?");
return redirect()->route('blogs');
} else {
return redirect()->back()->withErrors(['We could not log you in. Sorry.']);
}
The issue is with: return redirect()->route('blogs');
The error is: Route [blogs] not defined. Is there a specific thing I'm suppose to do? A specific way to call domain specific routes?
You would need to name a route 'blogs'. The redirect()->route('blogs') refers to a route name not a url/path.
For a url/path you can use redirect()->to($url) or just redirect($url).
Laravel - Routing - Named Routes

Resources