Reputation: 89
I have a route, based on the term i need to call the appropriate controller. for e.g
Route::get('/{term}','Usercontroller')
Route::get('/{term}','brandcontroller')
i want to achieve some thing like this. what the term holds is a name(string), based on this string it either belongs to User table or brand table. how can i achieve something like this using Service Container. that before deciding on which route to take, based on the term if it belongs to USER class, the usercontroller should be called, or if it belongs to brand class , brandcontroller route should be taken. Any help will be appreciated. Thanks
Upvotes: 4
Views: 676
Reputation: 3484
Create middleware IsBrand, & check if brand exists?
Route::group(['middleware' => 'IsBrand'], function () {
Route::get('{term}', 'BrandController');
});
Same goes for IsUser.
Route::group(['middleware' => 'IsUser'], function () {
Route::get('{term}', 'UserController');
});
Use php artisan make:middleware IsBrand
to create middleware.
This command will place a new IsBrand
class within your app/Http/Middleware
directory.
<?php
namespace App\Http\Middleware;
use Closure;
class IsBrand
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (App\Brand::where('brand_name', $term)->count())) {
return $next($request);
}
}
}
Upvotes: 4