Multiple Domains w/ Single Laravel Install - php

Sort of consolidating a few questions that are old, unresolved, and kind of in keeping with my own problem.
Routes file:
echo url()->current() ."<br>";
echo request()->getHost();
Route::domain('pro.local')->group(function () {
Route::get('/', function () {
dd('HELLO');
});
});
Route::group(['domain' => 'pro.local'], function() {
dd('PRO');
});
Route::group(['domain' => 'media.local'], function() {
dd('MEDIA');
});
Route::group(['domain' => 'software.local'], function() {
dd('SOFTWARE');
});
Route::get('/', function () {
return view('welcome');
});
Desire & environment: Three domains pro.local, media.local, and software.local all pointing to the same public folder using MAMP PRO 5.2 and Laravel 5.7. This is all I have done to the project so far.
Hypothesis: Using Route::domain or Route::group should result in returning the dd() text or the welcome template.
So far: I know the mono-repo setup I'm using works because I've had the three sites running off the mono-repo for about 3 years and can share the services and what not across projects. With that said, it's annoying to have to SSH into three separate folders to run composer update and npm update; especially when the composer.json and package.json files for each project is essentially the same...I currently use gulp to move and copy files around to keep things in sync.
Problem: No matter the domain, only PRO gets echoed.
It seems to skip the Route::domain and settle on the first Route::group, as demonstrated by moving the dd('MEDIA') call to the top.

Code inside a Route::group always gets run, as Laravel compiles the various route definitions for later use. Thus, your dd() is getting executed as Laravel builds the list of routes, short circuiting the entire process regardless of what domain you're on.
If you put each of your debugging dd calls within a Route::get('/', function () {}) inside each route group (like you do the first time with the Route::domain('pro.local') bit), you'd get the results you expected.
Route::group(['domain' => 'pro.local'], function() {
Route::get('/', function () {
dd('PRO');
});
});
Route::group(['domain' => 'media.local'], function() {
Route::get('/', function () {
dd('MEDIA');
});
});
Route::group(['domain' => 'software.local'], function() {
Route::get('/', function () {
dd('SOFTWARE');
});
});
Route::get('/', function () {
return view('welcome');
});
ALTERNATIVE: Switching them all to use Route::domain also ended up working per discovery on another forum.

Related

How should I set up Laravel routes?

I'm a beginner in Laravel and I'm having some difficulties with routes. Actually, I'm having difficulties with a lot of things in Laravel, some of which I've managed to wrap my head around (such as migrations, seeding and authentication) but this is one of the most basic ones.
I've been creating routes based on the one that comes with Laravel. However, after much googling, something seems off. I'm not sure this is how it should be done.
My current web.php file looks like this:
Route::get('/', function () {
return view('pages.home');
});
Route::get('/about', function () {
return view('pages.about');
});
Route::get('/login', function () {
return view('login');
});
Route::get('/student', function () {
return view('profiles.student');
});
Route::get('/professor', function () {
return view('profiles.prof');
});
Route::get('/profadmin', function () {
return view('profiles.profadmin');
});
Route::get('/ident', function () {
return view('pages.ident');
});
// Authentication
Auth::routes();
Route::post('/login', function () {
return view('pages.ident');
});
Route::get('logout', 'Auth\LoginController#logout');
// Route::get('/home', 'HomeController#index')->name('home');
// Route::get('/ident', 'HomeController#ident')->name('ident');
//
// Route::get('/aluno', 'HomeController#aluno')->name('aluno');
//
// Route::get('/ident', 'HomeController#ident')->name('ident');
Also, certain pages should only be viewed by authenticated users and I'm having a hard time understanding how exactly that is done and how the routes should reflect that.
I'm sorry if this is simple stuff, but this is my first time using a PHP framework. Any help will be much appreciated.
lets suppose you want to protect the about route
then in the web.php file, replace your about route with this:
Route::get('/about', function () {
return view('pages.about');
})->middleware('auth');
now anyone hits /about and not logged in, it will be redirected to /login
if you want to know more about authentication, Laravel documentation really the best place for you:
https://laravel.com/docs/5.5/authentication#protecting-routes
First if you are beginner you should read Laravel documentation & Laracasts
In your routes you trying to show only views
Route::get('/about', function () {
return view('pages.about');
});
In Laravel 5.6 you can do it like this
Route::view('/about', 'viewName');

Laravel single application but for front and admin area

If I will create some application on Laravel (for example it will be project.com) and in this same application I will develop admin area (with ACL, users management, etc.). Can I use it like project.com for front-side but backoffice.project.com for admin area in same application?
Thanks.
You can maintain both applications in the same Laravel project and use grouped routes and filter your routes by domain.
Route::group(['domain' => 'backoffice.project.com'], function () {
// your BACKEND routes...
});
Route::group(['domain' => 'www.project.com'], function () {
// your FRONTEND routes...
});
You can complement the route comportment with middleware too.
// in this case all backend routes will be passed to auth middleware.
Route::group(['domain' => 'backoffice.project.com', 'middleware' => 'auth'], function () {
// your BACKEND routes...
});
Important:
Observe that the Laravel documentation talk about Sub-Domains Routing. In this case, the approach of the documentation is the use of dynamic subdomains, as can be seen in the following example.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
In this case, {account} is a route parameter that can be used inside of the route group.
You can see (and read) more about Laravel routes here: https://laravel.com/docs/5.5/routing
Yes, you can group routes by domain.
Since Laravel 5.3 you can group them like this:
Route::domain('project.com')->group(function () {
// Your frontend routes
});
Route::domain('backoffice.project.com')->group(function () {
// Your backend routes
});
Before Laravel 5.3 you can group them like this:
Route::group(['domain' => 'project.com'], function () {
// You frontend routes
});
Route::group(['domain' => 'backoffice.project.com'], function () {
// You backend routes
});

Laravel Middleware / Route Groups

I'm fairly new to Laravel, so this question may obvious to some.
In the case of running checks per HTTP request, for example User Authentication. Is there a better, more efficient or simple correct way to run these checks. From my initial research it would seem that this could be accomplished using either MiddleWare, eg.
public function __construct()
{
$this->middleware('auth');
}
It also seems like it would be possible using routing groups, eg.
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
Is there any benefits of doing this either of these two ways? Apart from the obvious benefit of not having to put $this->middleware('auth'); in every controller auth would need to be checked.
Thanks
Edit..
After taking on your advice I attempted to utilities the route grouping to control my Auth MiddleWare. But this has seemed to have broken my site.
Route::group(['middleware' => 'auth'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get ( '/redirect/{provider}', 'SocialAuthController#redirect' );
Route::get ( '/callback/{provider}', 'SocialAuthController#callback' );
});
Am I missing something obvious?
You are almost there, just remove the Route::auth():
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'HomeController#index');
//add more Routes here
});
The suggested options did not work for me but when I checked the laravel documentation, I found this:
Route::middleware(['web'])->group(function () {
//Your routes here
});
It works for me. Laravel 8.*
There is no real difference, personally i use groups for the standard middleware and put exceptions in the construct
Using Route group is easy for maintenance/modification , other wise you will have to remember each controller where you are using certain middle ware, of course this not a concern in a small medium sized application, but this will be hard in a large application where is lots of controller and references to middle ware.

Combine AngularJS and Laravel Routing

I am using AngularJS and Laravel for my web app project. My routing look like this:
AngularJS:
angular.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: '/admin/dashboard'
});
}]);
Laravel:
Route::group(array('prefix'=>'admin', function(){
Route::get('/', ['as'=>'admin.main',function(){
return view('main');
}]);
Route::get('/dashboard', ['as'=>'admin.dashboard',function(){
return view('dashboard');
}]);
});
I am facing problem that I would need to declare route path at 2 place. One at Angular and the other one at Laravel. So, every time when I add new route or change route path, I will need to work on 2 place. This will become something tedious and hard to maintain when the app grows.
Is there anyway that I only need to set the route URL at one place, but will be effective for both?
I assume that you're building the single-page app. That means on server-side (Laravel) you need to use the same template for all GET requests, e.g.
Route::group(['prefix' => 'admin'], function() {
Route::get('(.*)', function() {
return view('dashboard');
});
});
On client-side (AngularJS) you're doing routing as described in question.
Btw, you're using wrong syntax in Laravel routing, this is incorrect:
Route::get('/', ['as'=>'admin.main',function(){
}]);
and this how it should be:
Route::get('/', ['as'=>'admin.main'],function(){
// ^
});
In Laravel 5.4 I was not able to define a route using regex as in:
Route::get('/account/(.*)', function() { return view('index'); });
Instead I had to use a parameter and use regex to ensure it captured all paths:
Route::get('/account/{path?}', function() {
return view('index');
})->with('path', '.*');

Laravel: How to Route to Public file

In Laravel, is it possible to redirect to a public/testFile.php, through routing?
In the application/routes.php,
Route::get('/', function()
{
//'How to point to public/testFile.php'
});
Have an existing Project, But want to do Only the NEW MODULES in Laravel. So copied the Existing project under Public/
You are completely defeating the purpose of the framework by doing this, but if you really want to...
Route::get("/", function() {
ob_start();
require(path("public")."testFile.php");
return ob_get_clean();
});
This will return the stdout output of the file. If instead you have a return value already in the script, knock out ob_start and the return call.
Redirects are done as follows:
Route::get("/", function() { return Redirect::to("testFile.php"); });
Route::get('/', function()
{
include public_path().'testFile.php';
});
If you want to Redirect then use return Redirect::to('testFile.php')
But I don't get why you want to do this weird thing.
I think you are using Laravel 3 (as you mentioned application/...), there public_path() is path('public').

Categories