in my Laravel 8 / tailwindcss 2 app
I use pagination and failed to create valid page url with routes :
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
// http://local-tads-back.com/admin/categories/page?=3
Route::get('categories/page?=/{page?}', [CategoryController::class, 'index'])->name('CategoriesFilter');
Route::resource('categories', CategoryController::class); // app/Http/Controllers/Admin/CategoryController.php
...
I got error :
Method App\Http\Controllers\Admin\CategoryController::show does not exist.
I do not have show method and try to set page condition before Route::resource ?
Is my condition invalid ?
Thanks!
you can use this
Route::resource('categories', CategoryController::class)->except(['show'])
ref link https://laravel.com/docs/8.x/controllers#restful-partial-resource-routes
Related
i have a larvel 8 project with other package and route and configured my route like this:
Route::group(['prefix' => config('site.route_prefix', 'site'), 'middleware' => config('site.route.middleware', 'web')], function () {
Route::get('/post/search/{q?}', [SearchController::class, 'search'])->name('site.search.query');
...
like this my search route not working, and the search method isnot handled.If i added some text to the route like this, then it work:
Route::group(['prefix' => config('site.route_prefix', 'site'), 'middleware' => config('site.route.middleware', 'web')], function () {
Route::get('/fdgfgfdgfdg/post/search/{q?}', [SearchController::class, 'search'])->name('site.search.query');
my question is where this behavior come from ?
In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});
When the user accesses "http://proj.test/" instead of getting the homepage I get:
Sorry, the page you are looking for could not be found.
But if the user accesses "http://proj.test/home" it works.
Also when the user accesses "http://proj.test/conference/create" instead of appear the page with the form to create a conference it appears:
View [app] not found. (View: /Users/johnw/projects/proj/resources/views/conferences/create.blade.php)
Do you know where can be the issue? Should be something about the links or routes but I don't know where is the issue.
Links that I'm using
<a class="logo" href="{{route('home')}}">Homepage</a>
Create Conference
Login
Logout
Register
Routes:
Route::group(['prefix' => '', 'middleware' => 'auth'], function(){
Route::post('/conference/store', [
'uses' => 'ConferenceController#store',
'as' => 'conference.store'
]);
Route::get('/conference/create', [
'uses' => 'ConferenceController#create',
'as' => 'conference.create'
]);
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
When the user accesses "http://proj.test/" instead of getting the homepage I get:
Sorry, the page you are looking for could not be found.
You have not defined any route for this URL
You can do
...
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index')->name('home');
...
Very simple answer - you need a Route::get('/', ...) route.
Add this to your Routes instead:
Route::group(['middleware' => ['guest']], function(){
Route::get('/', 'WelcomeController#index');
// WelcomeController is my own example, yours will differ...
});
This way it'll check to see if there's an auth or not and will redirect accordingly.
I am currently trying to set up breadcrumbs for my Laravel 5 application. Unfortunately, I am currently being presented with this error when I access localhost:8888/auth/login:
ErrorException in
/Users/ben/Sites/laravel/vendor/davejamesmiller/laravel-breadcrumbs/src/CurrentRoute.php
line 29
The current route (GET /auth/login) is not named - please check
routes.php for an "as" parameter
Routes.php:
Route::get('auth/login', 'Auth\AuthController#getLogin',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
The error is shown with or without the ['as' => 'login', 'uses' => 'Auth/AuthController#getLogin'] addition.
Breadcrumbs.php
Breadcrumbs::register('login', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Login', route('login'));
});
Thank you for your help.
I resolved this issue by changing the route to the following:
Route::get('auth/login',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
You can only declare which controller method you're using once.
I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.
Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.
Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.