Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Separate frontend and backend


Anyone can help me to separate frontend and backend routes in Metronic laravel version?
Thanks !


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (7)


I tried your version but for now not working. I will try to search for another method.



under routes/frontend create an auth.php file and add:


<?php

Route::group(["middleware" => "guest"], function () {
// Authentication
Route::get("login", [LoginController::class, "showLoginForm"])->name("login");
Route::post("login", [LoginController::class, "login"])->name("login.post");
});


then under routes/backend create an admin.php file and add for example:


<?php

Route::redirect("/", "/admin/dashboard", 301)->name("redirect");


that way you have your routes separated by frontend and backend happy



The way i manage separation for frontend and backend routes is as follows:

create a helper:


if (! function_exists("includeFilesInFolder")) {
/**
* Loops through a folder and requires all PHP files
* Searches sub-directories as well.
*
* @param $folder
*/
function includeFilesInFolder($folder)
{
try {
$rdi = new RecursiveDirectoryIterator($folder);
$it = new RecursiveIteratorIterator($rdi);

while ($it->valid()) {
if (! $it->isDot() && $it->isFile() && $it->isReadable() && $it->current()->getExtension() === "php") {
require $it->key();
}

$it->next();
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}

if (! function_exists("includeRouteFiles")) {

/**
* @param $folder
*/
function includeRouteFiles($folder)
{
includeFilesInFolder($folder);
}
}


then on routes/web.php:


/*
* Frontend Routes
*/
Route::name("frontend.")->group(function () {
includeRouteFiles(__DIR__."/frontend/");
});

/*
* Backend Routes
*/
Route::prefix("admin")->name("admin.")->middleware("auth")->group(function () {
includeRouteFiles(__DIR__."/backend/");
});


then under routes folder create the two folders (frontend and backend) and just create your route files in there, for example:

under routes/frontend create an auth.php file and add:


<?php

Route::group(["middleware" => "guest"], function () {
// Authentication
Route::get("login", [LoginController::class, "showLoginForm"])->name("login");
Route::post("login", [LoginController::class, "login"])->name("login.post");
});


then under routes/backend create an admin.php file and add for example:


<?php

Route::redirect("/", "/admin/dashboard", 301)->name("redirect");


that way you have your routes separated by frontend and backend happy



Thank you Carlos and appreciate your efforts. Hope this can help others as well happy

Thanks



Great.
I will try this
Thank you !



Hi,

You have to customize it. At the moment, Metronic does not have an example for it.

You could also split your admin routes into a separate file, and require that file in your app/routes.php file. With separate route for / and /admin.

Thanks



I tried to customize it, but when i change routes files and get back to initial code:

// Route::get('/', function () {
// return redirect('index');
// });

But when i try this, authentication and menu doesn't work anymore.

I don't know how to handle this:
$menu = theme()->getMenu();
array_walk($menu, function ($val) {
if (isset($val['path'])) {
$route = Route::get($val['path'], [PagesController::class, 'index']);

// Exclude documentation from auth middleware
if (!Str::contains($val['path'], 'documentation')) {
$route->middleware('auth');
}

// Custom page demo for 500 server error
if (Str::contains($val['path'], 'error-500')) {
Route::get($val['path'], function () {
abort(500, 'Something went wrong! Please try again later.');
});
}
}
});

I know how to do this with initial laravel files, but metronic was customized in another way.

Thanks !


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(