Separation of Admin Section on a different port in Laravel 5.x
I was working on a project where client wanted to have admin area on a separate port for privacy reasons.
I consulted old friend Google and searched for the any existing implementations. To my dismay, none of the solutions worked for me.
I then thought about the problem and opened RuoteServiceProvider
in app\Providers
directory in my project.
After a detailed inspection of the file, it hit me.
There is a map()
method in this class (RouteServiceProvider
) with the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
It means you can add your custom routes to it based on condition, so I quickly changed the implementation to the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function map()
{
switch(request()->getPort())
{
case 80:
case 8080:
case 443:
$this->mapApiRoutes();
$this->mapWebRoutes();
break;
// choose a port that is not used by another server
case 8975:
$this->mapAdminWebRoutes();
break;
}
}
Notice I’m scanning port number now for every request, that will have it’s own implications and a small over-head, also, I was unable to add an SSL certificate to this port as HTTPS uses port 443.
Please do comment if you know any technique to add SSL certificate to custom port or if I’m doing something wrong where
Now I added the following method maprAdminWebRoutes
to RouteServiceProvider
class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected function mapAdminWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/admin-web.php'));
}
Then I added admin-web.php
file to the routes
directory and added my routes to the file.
We’re not done yet!
In order for this to work, we also need to add port number to our nginx sites-enabled/YOUR-DOMAIN-CONF-FILE.conf
file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server {
listen 8975;
server_name YOUR_DOMAIN_NAME;
root '/path/to/your/application/public/;
...
And that’s pretty much it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server {
listen 8975;
server_name YOUR_DOMAIN_NAME;
root '/path/to/your/application/public/;
...
Now you can access your custom routes on your custom port(s).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server {
listen 8975;
server_name YOUR_DOMAIN_NAME;
root '/path/to/your/application/public/;
...
I hope you enjoyed this as much as I did when I implemented this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server {
listen 8975;
server_name YOUR_DOMAIN_NAME;
root '/path/to/your/application/public/;
...
Thanks for reading!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server {
listen 8975;
server_name YOUR_DOMAIN_NAME;
root '/path/to/your/application/public/;
...