My form validation was not displaying errors on my posts/create route. I Googled the solution and found that
Route::group(['middleware' => 'web'], function() {
Route::resource('/posts','PostsController');
});
can be changed to
Route::group(['middlewareGroups' => 'web'], function() {
Route::resource('/posts','PostsController');
});
I did that and the problem is solved now. I want to ask if this is considered a good practice to change it like this?
Also if I am removing this line from my routes it is working:
Route::group(['middleware' => 'web'], function() {
Can anyone tell me what is actually happening ?
One reason can be that web middleware is applying two times in your case. Laravel by default adds web Middleware in app/Providers/RoutesServiceProvider.php and again you are adding it in your routes.php
Try it by removing web middleware from your routes.php.
Well, Laravel 5.3 documentation explains it in detail.
Here is the link.
https://laravel.com/docs/5.3/middleware#middleware-groups
But to answer your question, this line:
Route::group(['middleware' => 'web'], function() {
doesn't allow any request to access the routes inside only if they validate a condition which is inside the "web" middleware.
This is how laravel works. The doc gives more detail about each component in the framework.
Related
I have been working on this project of mine now for sometime and has no problem with routes until today.
I have even tried clearing cache and dump autoloading. Nothing seems to be working.
I tried to add a new route today and i got a 404 error. I have also use "get" and "any", all to no avail.
At first, I have tried creating several new routes and I'm still getting the same 404 error. Below is how a part of my web.php looks like.
Route::group(['middleware' => ['auth', 'role:teacher']], function () {
Route::any('/testing', 'PagesController#testing');
Route::resource('/attendance', 'AttendanceController');
Route::get('/teacher/dashboard', 'TeachersController#dashboard')->name('teacher.dashboard');
Route::resource('homework', 'HomeworkController');
Route::resource('/teacher/events', 'EventsController',['names' =>
'teacher.events']);
Route::any('/view_students', 'StudentsController#myStudents')->name('view.students');
Route::resource('results', 'CoursesResultController');
Route::get('/results/class_course/{id}', 'CoursesResultController#showCourseResult');
Route::post('/results/class_course/{id}', 'CoursesResultController#saveCourseResult');
});
EDIT: I have solved the issue. I had to delete the cache files in the bootstrap folder manually. Thanks, guys.
Replace your code with the following:
Route::resource() generates all the possible routes for you, hence should be kept as the last possible point.
Route::group(['middleware' => ['auth', 'role:teacher']], function () {
Route::any('/testing', 'PagesController#testing');
Route::resource('/attendance', 'AttendanceController');
Route::get('/teacher/dashboard', 'TeachersController#dashboard')->name('teacher.dashboard');
Route::resource('homework', 'HomeworkController');
Route::resource('/teacher/events', 'EventsController',['names' =>
'teacher.events']);
Route::any('/view_students', 'StudentsController#myStudents')->name('view.students');
/* changes over here
`Route::resource()` generates all the possible routes for you, hence should be kept as the last possible point.
*/
Route::get('/results/class_course/{id}', 'CoursesResultController#showCourseResult');
Route::post('/results/class_course/{id}', 'CoursesResultController#saveCourseResult');
Route::resource('results', 'CoursesResultController');
});
Controller code: public function xyz(){echo 'hello';}
Route::group(['prefix' => 'api'], function(){Route::post('apiregstration','APIcontroller#xyz');});
I use laravel 5.1 and want to create API with post method but it not work ,
GET method work fine
If get is work but post not working, maybe you should try to run php artisan route:clearrun to clear route caches.
Looks like there is a typo in your snippet (which I'm assuming is coming directly from your source).
Try changing
Route::group(['prefix' => 'api'], function() {
Route::post('apiregstration','APIcontroller#xyz');
});
To
Route::group(['prefix' => 'api'], function() {
Route::post('apiregistration','APIcontroller#xyz');
});
I'm assuming you meant 'apiregistration' and not 'apiregstration'.
There is not much we can assume or test or check with the info you provided.
If the error doesn't go away, try adding a bit more of your code in the question so we can help.
I am looking to implement a whitelabel solution to my platform and need to implement wildcard subdomains for or system, the only issue is our system sits on a subdomain its self. So I need to filter out anything that comes to a specific subdomain.
// *.website.co.uk
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
// my.website.co.uk
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
However using the code above I get the error:
Undefined variable: element
How would I avoid this error?
A good way is to exclude 'my' using a pattern. Put this code at the top of your routes file:
Route::pattern('element', '(?!^my$)');
Alternatively, that can go in the boot() section of your RouteSericeProvider. To give you a working solution, your code becomes the following (you can tidy up later!)
Route::pattern('element', '(?!^my$)');
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
});
An alternative way is to match the 'my' route before matching the {element} route. However, while many do this I think it might be harder to maintain if the ordering of routes isn't clearly explained in the comments.
I'm fairly new to Laravel, so this question may obvious to some.
In the case of running checks per HTTP request, for example User Authentication. Is there a better, more efficient or simple correct way to run these checks. From my initial research it would seem that this could be accomplished using either MiddleWare, eg.
public function __construct()
{
$this->middleware('auth');
}
It also seems like it would be possible using routing groups, eg.
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
Is there any benefits of doing this either of these two ways? Apart from the obvious benefit of not having to put $this->middleware('auth'); in every controller auth would need to be checked.
Thanks
Edit..
After taking on your advice I attempted to utilities the route grouping to control my Auth MiddleWare. But this has seemed to have broken my site.
Route::group(['middleware' => 'auth'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get ( '/redirect/{provider}', 'SocialAuthController#redirect' );
Route::get ( '/callback/{provider}', 'SocialAuthController#callback' );
});
Am I missing something obvious?
You are almost there, just remove the Route::auth():
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'HomeController#index');
//add more Routes here
});
The suggested options did not work for me but when I checked the laravel documentation, I found this:
Route::middleware(['web'])->group(function () {
//Your routes here
});
It works for me. Laravel 8.*
There is no real difference, personally i use groups for the standard middleware and put exceptions in the construct
Using Route group is easy for maintenance/modification , other wise you will have to remember each controller where you are using certain middle ware, of course this not a concern in a small medium sized application, but this will be hard in a large application where is lots of controller and references to middle ware.
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel