How does Laravel 5.4 Middleware work? - laravel

I've read manual of Laravel 5 but still don't find a solution for my problem.
My task: I need to run multiple websites on the one codebase.
What have I done for it? I made one main website and create table "sites" where I can add new satellites. It's all worked fine. But now, in front end part, I need to create middleware which will help me to divorce different parts of my engine.
I start to create middleware but I've only found 2 ways of middleware:
redirect to the correct URL
return $next($request) which means that this middleware will be always working.
I don't need a redirect. I just need one thing:
I need that in my web.php route file I can make routes with my middleware group:
For example:
Route::group(['middleware' => 'site'], function () {
//there will be only routes for my satellites
And
Route::group(['middleware' => 'web'], function () {
//There will be another routes which won't be on satellites
The problem is if I make a redirect on the main site my middleware still working and going into the cycle.
Is there any way to break middleware and don't going into the certain routes?
I've tried with HttpException but I've received only 404 or 500 errors, but I don't need it in this case.

Related

Is it possible to make Single Page Application and MPA at the same time?

I am using Laravel and vue.js to make a SPA. So my routes looks like this:
In my routes/web.php
Route::get('/{any}' , 'SinglePageController#index')->where('any', '.*');
and then, the entire route will be handled by the vue router.
However, I decided to make a Multiple Pages(for my SEO) on other Pages while the SPA is for the loggedin users only.
I'm going to add another route in web.php but it returns 404 .
Route::get('/guests', 'GuestController#index');
[Note that I have GuestController and blades]
Is it possible? If so, please give me hints. I tried to search on google but haven't found.
Yes, you just have to place your route for /guests before your more general /{any}. Remember, Laravel check routes from top to bottom and the first matched will be used.
Route::get('/guests', 'GuestController#index');
// other specific routes
// ...
Route::get('/{any}' , 'SinglePageController#index')->where('any', '.*');

How do I move a request to API in laravel?

I can't seem to find an answer on the web or maybe I'm missing something.
Let's say my laravel application domain is my-laravel.com
Now I want to make requests at api.my-laravel.com which will work just same as my-laravel.com/api/
How do I do that?
Solution: Changed prefix('api') to domain('api.my-laravel.com') in RouteServiceProvider#mapApiRoutes
You may group your routes in a sub-domain, change your routes/api.php
Route::group(["domain" => "api.domain.key"], function() {
// your api routes.
});
Edit:
Check your RouteServiceProvider then remove the api prefix in mapApiRoutes method.
Alternatively(Except DNS and mod_rewrite)
Sub-Domain Routing
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified by calling the the domain method before defining the group:
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
Source: https://laravel.com/docs/5.4/routing#route-group-sub-domain-routing
Note:
Before this map your subdomain to point your server and point your domain (laravel.com)

Why my routes are not ordered?

in my routes file I use a route group with segment like:
Route::group(['prefix' => request()->segment(1) ], function(){
//routes
});
Normally, my routes are in the order they were written at, but when using the group with request()->segment(1) routes just get disarranged (not just inside the group itself, but all of them), I need to use segment so every customer will have their own slug as the first segment in the URL.
so how can I fix this issue?
Use middleware to validate the user and allow them based on their slug
Please Make own middleware like jwt.auth
I developed my own middleware to filter slug based

Laravel 5 - Assign Middleware to Controller Namespace

I need to do an ACL check for the user before allowing access to the admin panel of a Laravel 5 website. What is the best way to do this for an entire controller group in the namespace App\Http\Controllers\Admin\*? Ultimately, I'm looking for a "set and forget" method to do this, and middleware looks like the best option so far.
The initial idea was to assign a middleware to the admin route, but this does not prevent any other non-admin route from accessing the controllers. This means a route can still target the admin controllers and bypass the ACL check.
The next idea was to insert the assignment in the constructor on the controllers, but this would require each additional controller to explicitly include the middleware. This would require a developer to know that the middleware should be included, which allows them to miss it entirely. This also applies to using one base controller as the parent for all admin controllers, since the developer would need to know that the base controller should be extended. Right now, this looks like the best solution.
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin\*? Or, is there a better way for the ACL check to never need to be explicitly assigned to every admin controller?
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin*?
No
The most simplest approach you can do is create a base controller such as App\Http\Controllers\Admin\Controller and include the middleware
while all other App\Http\Controllers\Admin\* extends it.
Alternatively, while still adding App\Http\Controllers\Admin\Controller, you could instead inject the middleware through IoC Container.
App::afterResolving('App\Http\Controllers\Admin\Controller', function ($controller) {
$controller->middleware('acl');
});
EDIT
My previous answer didn't quite work for all situations; it broke a lot of other routes. Here's what I ended up doing instead (in app/Http/routes.php):
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'App\Http\Middleware\Acl'], function()
{
Route::get('/', 'AdminController#index');
Route::get('/user', 'User\UserController#index');
...
});
This at least targets all admin controllers when I define a route. It's not exactly everything I had hope for, but it will do.

Laravel Controller Delegation

Trying to keep my Laravel project organized here, while letting it grow.
Currently I use:
Route::controller('/admin', 'AdminController');
...in order to allow the controller to service general admin pages. This is working fine, however I'd like to delegate specific subqueries to other controllers for cleanliness reasons.
For example, I'd like /admin/dashboard to resolve to AdminController#getDashboard. I'd also like /admin/gallery/ to resolve to AdminGalleryController#getIndex, and /admin/foo/bar to resolve to AdminFooController#getBar.
Is there a simple way to slowly expand functionality like this?
We've migrated to Laravel 5 and 5.1, and this still remains a good way to do things. If you aren't using route groups in Laravel, then you aren't doing Laravel right.
You can define those others as controller routes as well. Just do it before Route::controller('admin') because Laravel searches the registered routes in the other you define them. Since /admin/gallery would match Route::controller('admin') as well as Route::controller('admin/gallery') latter has to be defined first:
Route::controller('admin/gallery', 'AdminGalleryController');
Route::controller('admin/foo', 'AdminFooController');
Route::controller('admin', 'AdminController');
Instead of writing admin every time a route group might be a nice improvement as well:
Route::group(['prefix' => 'admin'], function(){
Route::controller('gallery', 'AdminGalleryController');
Route::controller('foo', 'AdminFooController');
Route::controller('/', 'AdminController');
});
Yes. Simply declare your "exception" routes before your main controller route.
Route::get('/admin/gallery','AdminGalleryContoller#getIndex');
Route::get('/admin/dashboard','AdminController#getDasboard');
Route::controller('/admin','AdminController');

Resources