Redirect entire route group to another url? - laravel-4

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');
});
});

Related

Use different route namespace based on middleware in Laravel

I have the following code in my routes/web.php
Route::namespace('Admin')->middleware(['admin'])->group(function() {
Route::get('/posts', 'PostController#index');
});
Route::namespace('User')->middleware(['user'])->group(function() {
Route::get('/posts', 'PostController#index');
});
I wish to use the same uri "/posts" in both cases and keep the role logic (admin, user) out of the controllers, however, in this case, when I request the route "/posts" in always responds with the last one.
I can't seem to find information of what I am missing here.
use prefix for different route for admin and user
/admin/posts
Route::group(['namespace' => 'Admin','middleware=>'admin','prefix' => 'admin'],function() {
Route::get('/posts', 'PostController#index');
});
/user/posts
Route::group(['namespace' => 'User','middleware=>'user','prefix' => 'user'],function() {
Route::get('/posts', 'PostController#index');
});
You may try this one
Route::group(['prefix'=>'admin','middleware'=>'admin'],function (){
Route::get('/posts',['uses'=>' PostController#posts','as'=>'posts.index']);
});
Route::group(['prefix'=>'user','middleware'=>'user'],function (){
Route::get('/index',['uses'=>' PostController#posts','as'=>'posts.index']);
});

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

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 Routing - How to pass a variable to subroutes?

I'm using the group logic to filter the admin section of my website. I have a routing like this:
Route::group(array('before' => 'auth'), function() {
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() {
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
//other routes...
});
How to make $datas available to all routes that are included in my group ?
You can share variables:
View::share('datas', $datas);
return View::make('admin/dashboard');
As you stated that you'd like to include $datas in every route, you can make use of the use keyword:
Route::group(array('before' => 'auth'), function()
{
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() use ($datas)
{
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
});
You can learn about the use keyword here.

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