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
});
Related
Per laravel doc, I can add the auth middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
You could also do the following using the middleware static method of the Route facade:
Route::middleware(['middleware1', 'middlware2'])
->group(function () {
// Your defined routes go here
});
The middleware method accepts a single string for one middleware, or an array of strings
for a group of middleware.
Route::middleware(['auth:api'])->middleware(['second:middleware'])
->prefix('yourPrefix')->group(function () {
//Add your routes here
});
I am using Laravel 5.2 and Zizaco/entrust 5.2.x .
How to control role's access to different path?
For example:
The route of admin is like this:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::group(['prefix' => 'admin','namespace' => 'Admin'], function () {
Route::resource('dashboard', 'DashboardController');
});
});
I would want that the role admin can access http://www.example.com/admin/dashboard/ ,
other roles cann't access it,
What should I do?
You can check for the admin role in middleware. Since you are using zizaco/entrust you can check the role using hasRole.
Use this code in middleware web.
public function handle($request, Closure $next)
{
if (!$user->hasRole('admin')) {
return redirect('home');//Redirect to any page you wish.
}
}
I'm trying to work out if it's possible to use the regular Laravel Authentication routes with blade/views for basic Auth then load the SPA (Vue.js with it's own router) and make calls to the API via Dingo?
At the moment I have this at this top of my routes.php, which works:
// All routes through web middleware
Route::group(['middleware' => 'web'], function () {
// Authentication
Route::auth();
// Authenticated routes
Route::group(['middleware' => 'auth'], function () {
// Load main SPA
Route::get('/', 'AppController#spa');
});
});
app.domian.com/ is protected with Auth and thats the route which the SPA uses. I use the standard, built in, Laravel Auth pages (non SPA) so when the user is logged in or registered it allows access to the home route and loads the SPA.
What I would like to do is use Dingo from this point onwards. So calls to app.domian.com/api/* are all handled by Dingo.
I've added this to the same routes file:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->group(['middleware' => 'auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});
});
This doesn't seem to work.
It is even possible to use Dingo in this way or do I have to forfeit the built in Auth for something like JWT. I'm hoping to do that in the future, but for now I just need to get this working.
you need to replace auth with api.auth in middleware.
$api->group(['middleware' => 'api.auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});
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', '.*');
In Laravel PHP Framework, How can I apply a filter on all routes/pages of the website except one specific route/page?
Update:
It will be great if there is a way other than (route groups)?
use route groups
// unsecured routes.
Route::get('/', 'UserController#getLogin');
Route::group(array('before' => 'yourFilter'), function()
{
// secured by filter `yourFilter`.
Route::controller('route1', 'XxxController');
Route::post('user/save', function() {
// content
});
Route::get('user', 'UserController#getUser');
});