My project need new mirrors domain each weeks only for the API.
To do that, in the route file I add a Route::group for each domains.
Route::group(['domain' => 'domain2.com'], function(){
Route::group(['namespace' => 'Api', 'prefix' => 'r'], function() {
Route::get('/{hash}', 'ApiController#index');
});
Route::get('/', function(){
return view('errors.noshort');
});
});
Route::group(['domain' => 'domain1.com'], function(){
Route::group(['namespace' => 'Api', 'prefix' => 'r'], function() {
Route::get('/{hash}', 'ApiController#index');
});
Route::get('/', function(){
return view('errors.noshort');
});
});
How can I merge this two Route::group and the next ?
Can I get all the domains from my database to dynamically do the route ?
Directly you can not assign array to domain. It expects only single string parameters.
You can do like this. to assign Route::group store in your DB fetch it in your route file
Try this hope it helps.
like,
$domains = \App\Domain::all();
foreach ($domains as $domain) {
Route::group(['domain' => $domain->domainName], function(){
Route::group(['namespace' => 'Api', 'prefix' => 'r'], function() {
Route::get('/{hash}', 'ApiController#index');
});
Route::get('/', function(){
return view('errors.noshort');
});
});
}
1 link your domains like this:
$apiRoutes = function() {
Route::group(['namespace' => 'Api', 'prefix' => 'r'], function() {
Route::get('/{hash}', 'ApiController#index');
});
Route::get('/', function(){
return view('errors.noshort');
});
};
Route::group(['domain' => 'domain2.com'], $apiRoutes);
Route::group(['domain' => 'domain1.com'], $apiRoutes);
2 Difficult to understand what you mean by import your domains
Related
I'm trying to use a variable (which contains a value stored in the session) as a prefix to all my group routes, so I can make it more readable and clean.
Basically I want to transform this:
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('admin.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('admin.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('admin.settings');
});
Route::group(['prefix' => 'user', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('user.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('user.profile');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('user.settings');
});
Route::group(['prefix' => 'manager', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('manager.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('manager.profile');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('manager.settings');
});
Into something like this:
$role = session()->get('role');
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name($role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name($role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name($role.'.settings');
}
What's the correct way to do this?
Can you try like this. It's worked on me.
Open app/Http/Providers/AppServiceProvider
use Illuminate\Support\Facades\Session;
public function boot()
{
$role = Session::get('role');
app()->bind('role', function() use ($role){
return [
'role' => $role
];
});
}
In routes/web.php
define('role', app()->make('role')['role']);
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name(role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name(role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name(role.'.settings'); });
In blade;
{{ route(app()->make('role')['role'].'.dashboard') }}
You can try to send "app()->make('role')['role']" as a variable to your blade for more understanding.
Try this;
define('role', session()->get('role'));
While using;
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name(role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name(role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name(role.'.settings'); });
I have a controller That have multiple functions with the same router so I am getting error exception.
Please Guide me for This error
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
You can't, the solution is :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/chart','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
Or you can call multiple functions on the same url, one with "get" method, and an other with "post" for example :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::post('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
But the Route::post() is accessible only after a form submission with method post.
Is it possible use controller with different namespace in route group ?
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', ['namespace'=>'Controllers','uses'=>'SiteController#dashobard']);
Route::get('posts', 'PostsController#index');
});
As #TimLewis has mentioned in the comments it is possible.
(Assuming the full namespace for SiteController is App\Http\Controllers) The following should work:
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', '\App\Http\Controllers\SiteController#dashboard');
Route::get('posts', 'PostsController#index');
});
However, it would make more sense to split the routes up:
Route::group(['prefix' => 'dashboard'], function () {
Route::get('/', 'SiteController#dashboard');
Route::group(['namespace' => 'admin'], function () {
Route::get('posts', 'PostsController#index');
});
});
Hope this helps!
I am trying to set an isolated backend and frontend environments in the same Laravel5 application.
My ideal purpose is to get such isolation thru
subdomains
different guards(tables) for users at front and users at backend
Up now I've just defined my guard/providers at config/auth.php
and done :
Routing
Route::group(['domain' => 'front.mydomain.com'], function () {
Route::get('/', 'WelcomeController#index');
Route::group(['middleware' => ['web']], function () {
Route::auth();
});
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/home', 'HomeController#index');
Route::group(['prefix' => 'record'], function () {
Route::get('all','RecordController#all');
});
});
});
Route::group(['domain' => 'admin.mydomain.com'], function () {
Route::get('/', 'WelcomeController#index');
Route::group(['middleware' => ['web']], function () {
//SOLO RUTAS de pantallas de LOGIN
Route::get('/password/reset', function() {
return Response::view('errors.404', array(), 404);
});
Route::auth();
});
Route::group(['middleware' => ['web','auth:admin']], function () {
Route::get('/home', function() {
return view('admin.dashboard');
});
Route::group(['prefix' => 'record'], function () {
Route::get('all', 'RecordController#all');
Route::get('edit/{id}', 'RecordController#edit')->where('id','[0-9]+');
});
});
});
Config/session (extract)
'domain' => '.urbina.biz',
I was expecting to get errors because I haven't yet implemented model/tables for the admin part, BUT all I get is redirection loops , apparently because the cookie is not really isolating my subdomains, looks like it's mixing it all up :(
Some one may help ?
I made a simple login form. I log my user with :
Auth::loginUsingId($user->id, true);
But when I redirect the user to the ClientController I'm redirect to the login form, the Auth session is not persitent.
return redirect()->action('ClientController#index');
My routes :
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
});
Route::group(['prefix' => 'admin', 'middleware' => 'web'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => ['auth', 'web']], function() {
Route::get('/', 'ClientController#index');
});
The web middleware needs to kick in before the auth middleware because the web middleware is responsible for booting up your session. Switch the order around like this:
Route::group(['prefix' => 'client', 'middleware' => ['web', 'auth']], function() {
Route::get('/', 'ClientController#index');
});
However, while we're on this subject, you can nest route groups within another route group so to prevent an error like this in the future, I would just recommend nesting everything inside the web middleware like this:
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => 'auth'], function() {
Route::get('/', 'ClientController#index');
});
});