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!
Related
Route::group(['controller' => [AdminController::class], 'middleware' => ['role:administrator']], function () {
Route::get('/admin', 'index');
});
It doesn't recognize the AdminController
Route::controller(AdminController::class)
->middleware('role:administrator'))
->group(function () {
Route::get('/admin', 'index');
});
More information about route controller groups in https://laravel.com/docs/9.x/routing#route-group-controllers
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
I create the auth class from laravel and now i would like to know how i can put the /register page only for users who login in system?
my routes are:
Route::group(['middleware' => ['web']], function () {
//
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
//Route::get('/register', 'Auth\AuthController#getRegister');
});
For authenticate url just add a auth middleware, that will work after logged in a user. Follow the code below:
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/register', 'YourController#getRegister');
});
You have to use a middleware to filter the users.
In the case of authentication, there is a built-in middleware
called 'auth'
You can filter by middlewares in group as AnowarCst showed you
or in single routes like this:
Route::get('/register', [
'middleware' => 'auth',
'yourController#yourFunction'
]);
Read the docs to better understand
MIDDLEWARE.
Don't be scared, it's more simple than how it looks. :)
I solved the problem with this:
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'register', 'showRegistrationForm']]);
$this->middleware('auth', ['only' => ['register', 'showRegistrationForm']]);
}
And my routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'Auth\AuthController#getLogin');
});
Route::group(['middleware' => 'web'], function () {
Route::Auth();
Route::get('/dashboard', 'HomeController#index');
});
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/register', 'Auth\AuthController#showRegistrationForm');
});
Thanks guys.
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');
});
});