Route is not defined Inertia.js - laravel

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.

Related

Laravel Route Can Not Find By Route Name

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");
});

Laravel PUT routes return 404

I'm trying to update data in the database
but it returns error 404 not found in postman
Route::put('/products/{id}','productController#update');
Just add 'Accept: application/json' header to request.
Please provide more code so we can find exactly where your problem is.
I assume you wrote the route in the api.php file and not in web.php file.
If you do so, you must enter the route as api/products/1.
You have to be aware of the route group prefix as well if you are using it. Every routes inside a group prefix will be wrapped and requires the prefix string in the beginning every time you want to access it.
For instance:
in web.php file:
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/products/1".
and in the api php file (this is the more likely for new users have to be aware):
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/api/products/1" since the api.php file will automatically wrap your routes within an api prefix.
and one more thing you need to know, if you are using a getRoutesKeyName method on your model, you should follow wether the wildcard to use id's or maybe slug based on what you type inside the method.
For instance:
public function getRoutesKeyName(){
return 'slug';
}
# this will require you to type "products/name-of-product" instead of "products/1"

How to set a route parameter default value from request url in laravel

I have this routing settings:
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
});
so if I am generating a url using the 'action' helper, then I don't have to provide the storeId explictly.
{{ action('DashboardController#index') }}
I want storeId to be set automatically from the request URL if provided.
maybe something like this.
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
})->defaults('storeId', $request->storeId);
The docs mention default parameter though in regards to the route helper (should work with all the url generating helpers):
"So, you may use the URL::defaults method to define a default value for this parameter that will always be applied during the current request. You may wish to call this method from a route middleware so that you have access to the current request"
"Once the default value for the ... parameter has been set, you are no longer required to pass its value when generating URLs via the route helper."
Laravel 5.6 Docs - Url Generation - Default Values
In my Laravel 9 project I am doing it like this:
web.php
Route::get('/world', [NewsController::class, 'section'])->defaults('category', "world");
NewsController.php
public function section($category){}
Laravel works exactly in the way you described.
You can access storeId in your controller method
class DashboardController extends Controller {
public function index($storeId) {
dd($storeId);
}
}
http://localhost/admin/20 will print "20"

How does Laravel call a method in a controller via Route?

I cannot understand how Routing works. I tried to read Illuminate\Routing\Route but cannot grasp, how Laravel calls a method in a controller.
Lets say we have:
Route::get('/', 'WelcomeController#index');
I found out that you could call it like this
Route::get('/', function () {
App::call('App\Http\Controllers\WelcomeController#index, []);
});
But I cannot read anything near that in the Route.php file.
With out going into all the details, somethings to look at:
Router#dispatch -> dispatchRoute
Router#runRoute
Router#runWithinStack
Route#run
Route#runController
ControllerDispatcher#dispatch
Route#runCallable
Illuminate\Routing\Router Illuminate\Routing\Route Illuminate\Routing\ControllerDispatcher
This will lead you from dispatching to the actual call on the controller itself.
If you see App\Providers\RouteServiceProvider you can find protected $namespace = 'App\Http\Controllers'; and
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Which means all your web.php routes, for example Route::get('/', 'WelcomeController#index'); visit https:://your-domain/ send action in App\Http\Controllers\WelcomeController method index
It's a bit of a beast but in a nutshell you have:
index.php will capture the request and send it through the Http Kernel.
Http Kernel will try and dispatch the Request to the Router.
The Router will then find the applicable Route (if there is one), get all of the middleware for the Route, pipe the Request through them and finally run the Route.
This should then return a Response which ultimately gets returned in the index.php file which will send it on.
NB
"Running" the route will get the controller method or closure, resolve any dependencies and then call the closure/method.

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');
});
});
});

Resources