Is there any way I can do this configuration in routes.php:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
return homeController#guest<--- Specific Controller's method
}
else
{
return homeController#logged <--- Specific Controller's method
}
});
I don't want to use redirect since I want to keep mysite.com/ as the main address.
Of course you can do everything directly in the route like #Ferticidios answer or have only one controller method like #maytham suggests. But you can also do exactly what you asked for:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
return App::make('homeController')->callAction('guest', array());
}
else
{
return App::make('homeController')->callAction('logged', array());
}
});
You can do something like this:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
//Do stuff... get data
return Response::view('guest')->with($data);
}
else
{
//Do stuff... get data
return Response::view('logged')->with($data);
}
});
This is what filters were designed to do in Laravel. There is already on for the built in Auth or you can create your own
Route::get('user', array('before' => 'auth', function()
{
return App::make('homeController')->callAction('logged');
}));
The default filters can be adjusted in app/filters.php
http://laravel.com/docs/4.2/routing#route-filters
Related
I have a project which have multiple subdomains.
for example I have a subdomain for Students which goes to a student controller and it looks like this:
Route::domain('students.domain.test')->group(function () {
Route::get('/', function () {
return "done reaching the students page";
});
});
The second type of domains is "domain.test" and any subdomain which I'm checking in the request level and that's fine too.
Route::get('/', [HomeController::class, 'index'])->name('index');
But before the second type of domains I want to make subdomain for specific types of Entities which I have in the database.
Route::domain('{someTypes}.domain.test')
->group(function () {
Route::get('/', function () {
return "done reaching SomeTypes Page";
});
});
My Entity table have these attributes: Id, Title, Type "which I want to check if the type is 5".
I tried to use the middleware:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
return $next($request);
}
abort(404, 'Sorry, Request Not Found');
}
and I applied it to my routes like this:
Route::group([
'middleware' => ['type:journal']
],function () {
Route::get('/', function(){
return 'journals logic goes here';
});
});
and I have another middleware to ignore types like this:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
abort(404, 'Sorry, Request Not Found');
}
return $next($request);
}
and applied it to the other routes like this:
Route::group([
'middleware' => ['except_entity:journal']
], function(){
Route::get('/', function(){
return 'default pages when journals fails';
})->name('index');
I hope its clear what I'm trying to achieve.
First, you need a check what version laravel that you used?
You need to use Middleware. And I think, method to code with laravel 6, 7, or 8, is a little bit different.
Can you give us more information about your code, so we can help it easier?
Hello i have 2 level users:
1 Super admin who is able to see and do everthing
2 TD which is able to only see stuff and not change anything
I made 2 middleware:
1 auth.superadmin and 1 auth.td
My routes:
Route::group(['middleware' => ['auth.superadmin']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::get('/users/create-worker', 'UsersController#getcreateworker');
Route::post('/users/post-create-worker', 'UsersController#postcreateworker');
Route::get('/users/create-agent', 'UsersController#getcreateagent');
Route::post('/users/post-create-agent', 'UsersController#postcreateagent');
Route::get('/users-optima', 'UsersController#indexoptima');
Route::resource('/users', 'UsersController');
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
Route::group(['middleware' => ['auth.td']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::resource('/users', 'UsersController',
['only' => ['index']]);
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
My middelware:
superadmin
if (auth()->check() && auth()->user()->level == 1) {
return $next($request);
}
return abort(404, 'no entry to this page');
TD
if (auth()->check() && auth()->user()->level == 2) {
return $next($request);
}
return abort(404, 'no entry to this page');
I tried beginning with /Users.
TD can only see the index at /Users.
When i do it this way the auth.superadmin cannot see index#/users...
Am i doing it wrong?
Any help is appreciated.
You can modify your routes and its groups like this:
Route::group(['middleware' => ['auth.td']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::resource('/users', 'UsersController',['only' => ['index']]);
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
Route::group(['middleware' => ['auth.superadmin']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::get('/users/create-worker', 'UsersController#getcreateworker');
Route::post('/users/post-create-worker', 'UsersController#postcreateworker');
Route::get('/users/create-agent', 'UsersController#getcreateagent');
Route::post('/users/post-create-agent', 'UsersController#postcreateagent');
Route::get('/users-optima', 'UsersController#indexoptima');
Route::resource('/users', 'UsersController');
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
});
and your auth:td middleware should be like this:
if (auth()->check() && (auth()->user()->level == 1 || auth()->user()->level == 2)) {
return $next($request);
}
return abort(404, 'no entry to this page');
Just for your knowledge you can either remove the outer middleware (auth:td) as both users can use the routes under it. But I haven't done that because I think you have more users in your system.
Hope this helps!
So basically my app has two types of dynamic url..
app.com/{page}
app.com/{user}
Both having their own controllers
PageController#index
User\ProfileController#index
But I'm struggling to get this working.
I have tried a few different methods. Here are two I have tried..
Route::get('{slug}', function($slug) {
if (App\Page::where('slug', $slug)->count()) {
// return redirect()->action('PageController#index', [$slug]);
// return App::make('App\Http\Controllers\PageController', [$slug])->index();
return 'Page found';
} else if (App\User::where('username', $slug)->count()) {
// return redirect()->action('User\ProfileController#index', [$slug]);
// return App::make('App\Http\Controllers\User\ProfileController', [$slug])->index();
return 'User found';
} else {
return abort(404);
}
});
I feel I should be doing this with middleware/filters. Any help would be great. Thanks.
I think you could achieve what you after with Route::group using a middleware to filter if it is a page or a user.
Route::group(['middleware' => 'isPage'], function () {
Route::get('{slug}', ['as'=> 'pages.show', 'uses' => 'PageController#show']);
});
Route::group(['middleware' => 'isUser'], function () {
Route::get('{slug}', ['as'=> 'users.show', 'uses' => 'User\ProfileController#show']);
});
If you were using slugs for the Pages and ids for the Users, your idea of handling the issue might make more sense, but since you are using slugs for both the pages and the users, I strongly suggest you try a different approach. Why not declare two routes? Why not use the "show" methods of the respective controllers while you are at it, and keep in line with conventions for resources?
Route::get('pages/{slug}', ['as'=> 'pages.show', 'uses' => 'PageController#show']);
Route::get('users/{slug}', ['as'=> 'users.show', 'uses' => 'User\ProfileController#show']);
And if you really want to keep your "root-slug-respective-redirect" functionality you could write afterwards:
Route::get('{slug}', function($slug) {
if (App\Page::where('slug', $slug)->count()) {
return redirect(route('pages.show', $slug));
} else if (App\User::where('username', $slug)->count()) {
return redirect(route('users.show', $slug));
}
return abort(404);
});
I do advise against it though, as it seems like a waste of queries.
Here are the docs on Laravel RESTful resource controllers for good measure.
Goal: I want to make route filter in Laravel 4 using Route::group and Route::filter
Description
I have 2 types of user :
Internal
Distributor
For, Internal, I have 2 groups:
admin
regular
For Distributor, I have 4 groups:
gold
silver
bronze
oem
Eligible Route
OEM Distributor are eligible for only 5 routes.
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController#show'));
Route::get('distributors/{id}/edit', 'DistributorController#edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController#update'));
Route::get('catalog_downloads','CatalogDownloadController#index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController#file_download');
Regular Distributor are eligible for 8 routes.
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController#show'));
Route::get('distributors/{id}/edit', 'DistributorController#edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController#update'));
Route::get('catalog_downloads','CatalogDownloadController#index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController#file_download');
Route::get('marketing_materials','MarketingMaterialController#index');
Route::get('marketing_materials/{id}/download/thumb_path','MarketingMaterialController#thumb_download');
Route::get('marketing_materials/{id}/download/media_path','MarketingMaterialController#media_download');
Code
filters.php
routes.php.
Questions
Can someone please help me or at least direct me to the right direction ?
First off: It's not possibble to declare a route that results in the same URL twice. Whether it's in a group or not. (Well if you have a group with prefix it's possible because a prefix changes to URL of the route)
You have to solve this problem by intelligent filtering
This is the simplest solution I've come up with:
Route::filter('distributor', function(){
$user = Auth::user();
if($user->type == "Distributor"){
return true;
}
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
});
Route::filter('distributor.regular', function(){
$user = Auth::user();
if($user->type == "Distributor"){
if($user->distributor()->type != 'OEM'){
return true;
}
}
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
});
The distributor filter checks just if the user is of type Distributor. The second filter, distributor.regular, checks if the distributor is not an OEM. (If you're wondering, the dot in distributor.regular has no special function or deeper meaning. I just like to write it like that)
Route::group(['before' => 'distributor'], function(){
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController#show'));
Route::get('distributors/{id}/edit', 'DistributorController#edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController#update'));
Route::get('catalog_downloads','CatalogDownloadController#index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController#file_download');
Route::group(['before' => 'distributor.regular'], function(){
Route::get('catalog_downloads', 'CatalogDownloadController#index');
Route::get('catalog_downloads/{id}/download', 'CatalogDownloadController#file_download');
Route::get('marketing_materials', 'MarketingMaterialController#index');
Route::get('marketing_materials/{id}/download/thumb_path', 'MarketingMaterialController#thumb_download');
Route::get('marketing_materials/{id}/download/media_path', 'MarketingMaterialController#media_download');
});
});
This should already work with the use-cases you posted. However we can make the filters more flexible and also reduce redundant code.
function makeError404(){
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
}
Route::filter('distributor', function(){
$user = Auth::user();
if($user->type == "Distributor"){
return true;
}
return makeError404();
});
Route::filter('distributor.group', function($route, $request, $value){
$groups = explode(';', $value);
$user = Auth::user();
if($user->type == "Distributor"){
if(in_array($user->distributor()->type, $groups)){
return true;
}
}
return makeError404();
});
Now we can dynamically specify in which group the user has to be...
Route::group(['before' => 'distributor'], function(){
// distributor routes
Route::group(['before' => 'distributor.group:gold;silver;bronze'], function(){
// regular routes
});
});
You can follow a path like this
class UserController extends BaseController {
/**
* Instantiate a new UserController instance.
*/
public function __construct()
{
$this->beforeFilter('employee', array('only' => 'index'));
}
}
I am using Laravel 4. When "logged_in_id" is found in session I want it to continue to parse through bellow written Routes.
Route::get('/{anything}', function($anything)
{
if(!Session::has('logged_in_id')) {
return View::make('user.login');
} else {
//continue to check Route::get written bellow this Routing
}
})->where('anything', '[A-Za-z0-9\/?=]+');
If I write Redirect::to('/'.$anything) then it enters the same Route and keeps on redirecting in loop. Is there any way to solve this problem?
I would create a filter and then apply it to any routes that need it.
Remember to put first the routes that are more restrictive and last the more generic ones.
See the example:
Route::filter('logged_in', function()
{
if(!Session::has('logged_in_id')) {
return View::make('user.login');
}
});
Route::get('testing', array(
'before' => 'logged_in',
function()
{
return View::make('user.testing');
}
));
Route::get('/{anything}', array(
'before' => 'logged_in',
function($anything)
{
return View::make('user.anything');
}
))->where('anything', '[A-Za-z0-9\/?=]+');