How to create a Route in Laravel to this scenario? - laravel

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]+');

Related

Laravel 5.2 to 5.3 - converting routes.php to web and api route files?

I am upgrading my guitar lessons site from 5.2 to 5.3. My previous developer has a full time job so now I am the site owner and developer... I am a better guitarist than developer...
My site has a backend for adding guitar lesson content. The routes.php file, among other things, had these lines:
function RegisterResourceRoute($route, $name) {
Route::get($route, 'Api\\'.$name.'Controller#getAll');
Route::get($route.'/{id}', 'Api\\'.$name.'Controller#get');
Route::post($route, 'Api\\'.$name.'Controller#post');
Route::put($route.'/{id}', 'Api\\'.$name.'Controller#put');
Route::delete($route.'/{id}', 'Api\\'.$name.'Controller#delete');
}
// Api
Route::group(['middleware' => ['api', 'auth', 'admin']], function () {
Route::group(array('prefix' => 'api'), function() {
RegisterResourceRoute('file', 'File');
RegisterResourceRoute('photo', 'Photo');
RegisterResourceRoute('category', 'Category');
RegisterResourceRoute('tag', 'Tag');
RegisterResourceRoute('lesson', 'Lesson');
RegisterResourceRoute('exercise', 'Exercise');
RegisterResourceRoute('user', 'User');
});
Route::post('/api/email', 'Api\\EmailController#send');
});
Then I see in the angular related js used in the view, this for the Exercise resource:
// Exercise
.when('/exercise', {
templateUrl: 'views/dashboard/exercise/view-all.html',
controller: 'ExerciseViewAllController'
})
.when('/exercise/create/:lessonId?', {
templateUrl: 'views/dashboard/exercise/create.html',
controller: 'ExerciseCreateController'
})
.when('/exercise/view/:id', {
templateUrl: 'views/dashboard/exercise/view.html',
controller: 'ExerciseViewController'
})
.when('/exercise/edit/:id', {
templateUrl: 'views/dashboard/exercise/edit.html',
controller: 'ExerciseEditController'
})
.when('/exercise/edit/:id/details', {
templateUrl: 'views/dashboard/exercise/edit-details.html',
controller: 'ExerciseDetailsController'
})
.when('/exercise/edit/:id/photos', {
templateUrl: 'views/dashboard/exercise/edit-photos.html',
controller: 'ExercisePhotosController'
})
.when('/exercise/delete/:id', {
templateUrl: 'views/dashboard/exercise/delete.html',
controller: 'ExerciseDeleteController'
})
The ViewAllController, for instance, looks like this:
angular.module('dashboard')
.controller('ExerciseViewAllController', ['$scope', '$http', function ($scope, $http) {
$scope.exercises = [];
$scope.refresh = function () {
$scope.exercises = [];
load();
};
var load = function () {
$scope.busy = true;
$http.get('/api/exercise').then(function (response) {
$scope.busy = false;
$scope.exercises = response.data;
}, function (response) {
$scope.busy = false;
});
};
load();
}]);
I am trying to figure out how 5.3 handles such routing. In my backend dashboard, when I try and view the list of exercises, for instance, I was getting a 404 error for api/exercise.
So I created web.php with this under admin routes:
Route::resource('api/exercise', 'Api\ExerciseController');
And in api.php
Route::group(['prefix' => 'api'], function () { Route::resource('exercise', 'Api\ExerciseController'); });
It now works, but I had to change what was previously a getAll() function in the Exercise controller to index(), otherwise there was a error since laravel was looking for the default index method.
I would rather get it working using default route names, and also I have no idea if what I added to web.php and api.php is really the correct way. I see things seem to work but I am worried I am not seeing the whole picture...
Can someone explain what I would do to get these routes working in the true laravel 5.3 way?
thanks!
Not remember exactly but I upgraded from 5.2 to 5.3 by this link https://laravel.com/docs/5.3/upgrade
Do not get overwhelmed by the content. just search for bits that you are getting errors from. for example routes section.
Hope that helps

Redirect entire route group to another url?

Given a route group like Route::group(['prefix' => 'foo',]) how would I redirect all routes under this group to domain.com?
I've tried:
Route::group(['prefix' => 'foo'], function() {
Route::get('/', function () {
return Redirect::to('http://domain.com');
});
});
But that would only redirect myoldsite.com/foo to domain.com, anything such as myoldsite.com/foo/deeper/path would just be a 404. Is there a way to catch all sub routes and redirect them?
You can add a catch-all parameter.
Route::group(['prefix' => 'foo'], function() {
Route::get('/{atsl}', function ($atsl) {
return Redirect::to('http://domain.com');
});
});

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 access controllers using different routes depending on user group

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

how to use the laravel subdomain routing function

I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

Resources