Laravel access controllers using different routes depending on user group - laravel-5

I currently have a booker and admin user groups. A booker user is assigned to 1 event, and an admin user is not assigned to any event.
Both user groups have route groups like this:
Route::group(['middleware' => 'booker'], function() {
Route::controller('event', 'EventController');
});
Route::group(['middleware' => 'admin'], function() {
Route::controller('admin', 'AdminController');
});
Actions in EventController are like this:
getIndex /event
getOverview /event/overview
postOverview /event/overview
getFacilities /event/facilities
postFacilties /event/facilities
etc.
When logged in as admin user group, is it possible for me to use the EventController actions for routes like this:
/admin/events/1
/admin/events/1/overview
/admin/events/1/facilties
/admin/events/1/schedule
etc.
Where, instead of getting the event id from the user, I would get it from the URL.
Thanks

Ok, I managed to get there, although it's quite messy so I'd still love to know if there is a nicer solution:
routes:
Route::group(['middleware' => 'booker'], function() {
Route::controller('event', 'EventController');
});
Route::group(['middleware' => 'admin'], function() {
Route::controller('admin/events/{id?}', 'EventController');
Route::controller('admin', 'AdminController');
Route::get('event', function() { return redirect('admin'); });
});
In EventController (using Sentry):
public function __construct()
{
$user = Sentry::getUser();
if($user->hasAccess('admin')) {
$id = Request::segment(3);
$event = Event::find($id);
if(!$event) {
abort(404);
}
} else {
$event = $user->event;
}
$this->data['event'] = $event;
}
public function getIndex()
{
return view('event.index', $this->data);
}
So as you can see I've had to use the URL segment for the id, as I couldn't find a way to pass the id variable in the route. The route variable just enables me to ignore the id and load the controller.
I found I couldn't use URLs like this within EventController views:
<a href="{{ URL::action('EventController#getOverview') }}">Overview<a>
// /event/overview
// /admin/events//overview
However using other helpers I could hack it in:
<a href="{{ URL::full().'/overview' }}">Overview<a>
// /event/overview
// /admin/events/3/overview

Related

How to create a Route in Laravel to this scenario?

The basic idea is to work like a sub-domain architecture, but the user account will come after website's name.
I have a web service that is called myService.com.
I will have users like Paul, Anne, Jhon.
I want Laravel to be able to create home pages for Paul, Anne and Jhon like this:
myService.com/Paul = > Should redirects to Paul home page
myService.com/Anne = > Should redirects to Anne home page
myService.com/Jhon = > Should redirects to Jhon home page
but myService.com has its own home page and others pages too. I do like this:
Route::get('/', function () {
return view('home');//myService.com HOME page
});
Route::group(['prefix'=>'videos'], function (){
Route::get('/', function () {
return "all videos";
});
Route::get('/{id}', function ($id) {
return "Video => $id";
});
});
Route::group(['prefix'=>'articles'], function (){
Route::get('/', function () {
return "all articles";
});
Route::get('/{id}', function ($id) {
return "Article => $id";
});
});
For the users I will have almost the same URL structure:
myService.com/Paul
myService.com/Paul/videos
myService.com/Paul/videos/id
myService.com/Paul/articles
myService.com/Paul/articles/id
myService.com/Paul2
myService.com/Paul2/videos
myService.com/Paul2/videos/id
myService.com/Paul2/articles
myService.com/Paul2/articles/id
Since I will have unlimited users account I dont know how to design the Route structure for this scenario.
Is It should be like this?
Route::group(['prefix'=>'account'], function (){
Route::get('/', function ($account) {
return "user $account home page";//Ex myService.com/Paul
});
Route::group(['prefix'=>'videos'], function (){
Route::get('/', function () {
return "user account - all videos";//Ex myService.com/Paul/videos
});
Route::get('/{id}', function ($id) {
return "user account video => $id";//Ex myService.com/Paul/videos/id
});
});
Route::group(['prefix'=>'articles'], function (){
Route::get('/', function () {
return "user account - all articles";//Ex myService.com/Paul/articles
});
Route::get('/{id}', function ($id) {
return "user account articles => $id";//Ex myService.com/Paul/articles/id
});
});
});
This is what I did so far, but I dont know if it is a good approach. It is correct?
Define that group at the end of your routes so the other routes will have precedence. Then you can use slug for username (or whatever you are using)
Route::group(['prefix' => '{username}'], function(){
Route::group(['prefix' => 'videos'], function(){
...
});
});
But this groupe is more likely to try and pick up all your 404. Maybe you can add extra constraint
Route::group(['prefix' => '{username}'], function(){
...
})->where('username', '[A-Za-z]+');

How to get routes list by specific groups in laravel 5?

Hello I am trying to do it like this but it's getting all the routes, I only want the routes from a specific group(s).
This is my code:
<?php
$routes = Routes::getRoutes();
#foreach($routes as $route)
{{ $route->getPath() }}
#endforeach`
Thanks in advance!
Let's create some routes without any groups
Route::get('/', function () {
return view('welcome');
});
Route::get('/load', 'defaultController#load');
Now we'll create some routes with groups
Route::group(['as' => 'admin'], function () {
Route::get('users', function () {
return "users route";
});
Route::get('ravi', function () {
return "ravi route";
});
Now we are going to create a route in this group which will look for the admin group and print all routes that exist in this group.
Route::get('kumar', function () {
$name = 'admin';
$routeCollection = Route::getRoutes(); // RouteCollection object
$routes = $routeCollection->getRoutes(); // array of route objects
Now in our route object, we will look for our named route by filtering the array.
$grouped_routes = array_filter($routes, function($route) use ($name) {
$action = $route->getAction(); // getting route action
if (isset($action['as'])) {
// for the first level groups, $action['as']
// will be a string
// for nested groups, $action['as'] will be an array
if (is_array($action['as'])) {
return in_array($name, $action['as']);
} else {
return $action['as'] == $name;
}
}
return false;
});
// Here we will print the array containing the route objects in the 'admin' group
dd($grouped_routes);
});
});
Now you can copy and paste this in your route folder and you will be able to see the output by hitting your_project_public_folder_url/kumar
I took help from this answer Answer of patricus

Session doesn't work in Laravel 5.2

I have a big problem with session in Laravel 5.2. My session doesn't set in some route.
Like this
Route::post('add','SiteController#add');
This is my route.php:
Route::get('admin','AdminController#index');
Route::resource('admin/product','ProcuctController');
Route::resource('admin/news','NewsController');
Route::resource('admin/category','CategoryController');
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
Route::get('/{title}','SiteController#show');
Route::group(['middleware' => ['web']], function () {
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
});
My file shopping cms basket doesn't work. This is my SiteController function
public function add(Request $request)
{
if(session::has('cart'))
{
$cart=session::get('cart');
if(array_key_exists($request->product_id,$cart))
{
$cart[$request->product_id]++;
}
else
{
$cart[$request->product_id]=1;
}
session::put('cart',$cart);
//var_dump(session::get('cart'));
print 'ok';
}
else
{
$cart=array();
$cart[$request->product_id]=1;
session::put('cart',$cart);
var_dump(session::get('cart'));
}
}
Every time that I click the buy button, condition doesn't return true
In your router.php you register add and session route twice. one is inside of web middleware and another is outside. Remove the outside one. so your router.php will look like
Route::get('admin','AdminController#index');
Route::resource('admin/product','ProcuctController');
Route::resource('admin/news','NewsController');
Route::resource('admin/category','CategoryController');
Route::get('/{title}','SiteController#show');
Route::group(['middleware' => ['web']], function () {
Route::get('session','SiteController#session');
Route::post('add','SiteController#add');
});
And in your SiteController add this line in to top
use session;

Laravel routing groups

I have a question about routing groups. I have two types of users and I can not use the role system. Following the laracast email verification video I was able to get a new type of user to work. So I can login and register no problem. However when I have both types of users routes going it starts rejecting logins and such.
I even tried separating the admin user routes and putting the artist user routes on a different php document but still will not allow two types of logins or to view the proper dashboard.
I have tried using namespace inside the group, prefix, and tried middleware to no avail.
Here's the routing code.
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('/artist', function () {
return view('artist');
});
Route::get('/sponsor', function () {
return view('sponsor');
});
Route::get('/viewer', function () {
return view('viewer');
});
Route::get('/contact', function () {
return view('contact');
});
// This is for the Artist Linkings
//Route::group(['middleware' => 'artist'], function () {
//Route::auth('artist');
//Route::get('art/dashboard', 'SessionsController#index');
//Route::get('art/dashboard', ['middleware' => 'artist', function() {
//return view('art/dashboard');
//}]);
//});
//Route::get('art/register', 'RegistrationController#register');
//Route::post('art/register', 'RegistrationController#postRegister');
//Route::get('register/confirm/{token}', 'RegistrationController#confirmEmail');
//Route::get('art/login', 'SessionsController#login');
//Route::post('login', 'SessionsController#postLogin');
//Route::get('/logout', 'SessionsController#logout');
//Route::get('art/dashboard', 'SessionsController#index');
//});
// Need to add the password stuff ect
// Route::group(['prefix' => 'viewer', 'namespace' => 'Viewer'], function () {
// require app_path('Http/Routes/viewers.php');
// });
// This is for all the Viewer Linkings
Route::get('viewer/register', 'ViewerRegistrationController#register');
Route::post('viewer/register', 'ViewerRegistrationController#postRegister');
Route::get('viewer/register/confirm/{token}', 'ViewerRegistrationController#confirmEmail');
Route::get('viewer/login', 'ViewerSessionsController#login');
Route::post('login', 'ViewerSessionsController#postLogin');
Route::get('/logout', 'ViewerSessionsController#logout');
Route::get('viewer/dashboard', 'ViewerSessionsController#index');
//});
//}]);
You can use this .
For artist user as you define
Route::group(["middleware" => ["auth.artist"], "prefix" => "artist","namespace"=>"Artist"], function() {
Route::controller('artist', 'UsersArtistController');
Route::controller('controles', 'controlsArtistController');
});
For viewer user if no any auth needed
Route::group("prefix" => "viewer","namespace"=>"Viewer"], function() {
Route::controller('Viewer ', 'UsersViewer Controller');
Route::controller('controles', 'controlsViewerController');
});
Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware'=>['auth','admin']], function (){
Route::get('dashboard','DashboardController#index')->name('dashboard');
Route::resource('tag','TagController');
Route::resource('category','CategoryController');
});
Route::group(['middleware'=>['auth']], function(){
Route::post('favorite/{post}/add','FavoriteController#add')->name('post.favorite');
Route::post('review/{id}/add','ReviewController#review')->name('review');
});
Route::group(['as'=>'user.','prefix'=>'user','namespace'=>'Author','middleware'=>['auth','user']], function (){
Route::get('dashboard','DashboardController#index')->name('dashboard');
Route::resource('post','PostController');
});

laravel 4 filter route not working

I have an admin section in my app which works fine, however now I want to close it so only admin has access. Basically I want to redirect to admin/login if a non-admin person is trying to access admin pages.
This is my admin route (which works):
Route::controller('admin', 'AdminController');
I tried adding the following filter above the admin controller route, and I also commented out the local array element so the environment is set to production, but it is just not working.
Routes.php
Route::filter('isAdmin', function()
{
if ( ! Session::has('admin') ) {
return Redirect::to('admin/login');
}
});
Route::get('admin', array('before' => 'isAdmin', function()
{
echo 'You are over 200 years old!';
}));
Route::controller('admin', 'AdminController');
So if now I go to mysite/admin , I still get to admin dashboard even though session admin does not exist.
Something like this should work, this is untested
Filters.php
Route::filter('isAdmin', function()
{
if ( ! Session::has('admin'))
{
return Redirect::to('admin/login');
}
});
Routes.php
Route::group(['before' => 'isAdmin'], function()
{
Route::get('admin', 'AdminController#index');
});
AdminController.php
<?php
class AdminController
{
public function index()
{
echo 'they\'re an admin';
}
}

Resources