laravel: Route [dashboard] not defined - laravel

when admin login then he redirects to dashboard page but by clicking on its main button(like home button) it shows error, i added this code: <a href="{{route('dashboard')}}"> in it but it says:
Route [dashboard] not defined
this is my route:
Route::get('/admin/dashboard','AdminController#dashboard');
any solution to resolve this issue

Give the name to Route.
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard);
or
Route::get('/admin/dashboard',[
'uses' => 'AdminController#dashboard',
'as' => 'dashboard']);

As shown in the docs:
route()
The route function generates a URL for the given named route:
$url = route('routeName');
as we see, it generates a URL for the given named route, so you have to provide a name for your route like the following from docs too:
Named Routes
Named routes allow the convenient generation of URLs or
redirects for specific routes. You may specify a name for a route by
chaining the name method onto the route definition:
Route::get('user/profile', function () {
// })->name('profile');
You may also specify route names for controller actions:
Route::get('user/profile', 'UserProfileController#show')->name('profile');
So add a name to your route:
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard');
Hope it helps

Related

Route Name Prefix: Why can't I use "admin.home" in Laravel 9 in grouped routes?

Route::group(['controller' => AdminController::class, 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('/', 'index')->name('home');
});
This is what I'm using, based on about 20-25 searches it should work, so I should be able to reach a route by using route('admin.home') in blade. However it says that admin.home is not defined.
Why is "as" nor the "prefix" not working? I honestly don't get it, I literally copy-pasted an "accepted answer"'s code and it still doesn't work...
Solution
Route::name('admin.')
->controller(AdminController::class)
->prefix('admin')
->group(function () {
// Matches the "/admin" URL
// with route name "admin.home"
// pointing to AdminController::index(...) method.
Route::get('/', 'index')->name('home');
});
Reference(s)
Route Name Prefixes
The name method may be used to prefix each route name in the group
with a given string. For example, you may want to prefix all of the
grouped route's names with admin. The given string is prefixed to
the route name exactly as it is specified, so we will be sure to
provide the trailing . character in the prefix:
Route::name('admin.')->group(function () {
Route::get('/users', function () {
// Route assigned name "admin.users"...
})->name('users');
});
Controllers
Route::controller(OrderController::class)->group(...);
Middleware
Route::middleware(['first', 'second'])->group(...);
Route Prefixes
Route::prefix('admin')->group(...);
Route Groups
Route::group(...);
Addendum
Clear/refresh your route cache if cached previously:
Terminal Command:
php artisan route:clear

Laravel Route Controller issue

I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.

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"

Side By Side Static Routes & Wild Card Routes In Laravel

I have a use case for a Laravel website I am working on to have some static routes sitting at the exact same level as the main wild card route.
EG:
Route: /store/cart Static Route
Route: /store/checkout Static Route
Route: /store/* Dynamic Route
Route: /store// Dynamic Route
Route: /* Dynamic Route
Been trying to figure out how to implement this routing structure in Laravel and while the static routing rules work fine as soon as I add the wild card routes I wind up with the wild card route trying to catch the static routes as well.
How would I be able to add routing rules to support this?
change the conflicting routes to non conflicting.
Route: /store/cart Static Route => this is ok
Route: /store/checkout Static Route => this is ok
Route: /store/* Dynamic Route => /store/id/{id}
Route: /store// Dynamic Route => /store
Route: /* Dynamic Route => remove this and be specific by having more routes for the needs
Us global constraint
Route::pattern('all','.*');
Then define your routes in order
Route::('store/cart', function () {});
Route::('store/checkout', function () {});
Route::('store', function () {});
Route::('store/{all}', function ($all) {});
Route::('{all}', function ($all) {});
Managed to get it working.
The first step was to use global constraint route patterns per #Aboalnaga...
Route::pattern('variableName','.*');
Each variable was defined with this route pattern to make it a wild card pattern.
The next step was to ensure route ordering. It appears as though when handling routing Laravel will work its way through the route list in order. As soon as it finds the first matching route it will stop there and run that route. So in order to handle a route chain in the form of domain.com/store/cat-1/productwhere the request could be for domain.com, domain.com/store, domain.com/store/cat-1, domain.com/store/cat-1/product, or domain.com/some-content-page-from-database the route needed to be defined as...
Route::get('/store/shopping-cart', 'onlineStore#showCart');
Route::get('/store/checkout', 'onlineStore#showCheckout');
Route::get('/store/checkout/payment', 'onlineStore#showPayment');
Route::get('/store/checkout/success', 'onlineStore#showPaymentSuccess');
Route::get('/store/checkout/error', 'onlineStore#showPaymentError');
Route::get('/store/{category}', 'onlineStore#showCategory');
Route::get('/store/{category}/{product}', 'onlineStore#showProductDetails');
Route::get('{article}', 'articles#showArticle');
By defining the routes in order and having the variable route defined as the last route at that level the variable route will only be triggered if the preceding routes don't match.

Laravel form open with multiple parameters

I am trying to pass variables with form open with following code:
{{ Form::open(['method' => 'PATCH','route' => ['note.update','project','1','1']]) }}
Here is my NoteController.php file
Class NoteController extends BaseController{
public function update($belongs_to,$unique_id=0,$note_id=0){
return $unique_id;
}
}
routes.php file is
Route::resource('note', 'NoteController');
Why I am only able to access $belongs_to variable and $unique_id and $note_Id are always 0 as given as default value??
That's because the routes registered with Route::resource only take one url parameter.
Take a look a this
So what you need to do is use this route:
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
If you want to keep the other routes from Route::resource just add it before Route::resource
Route::patch('note/{belongs_to}/{unique_id?}/{note_id?}', 'NoteController#update');
Route::resource('note', 'NoteController');
If you don't want to add the route like this, you'll have to use query parameters to pass additional information

Resources