Skip to content

Commit

Permalink
auto registration of routes in domains
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Apr 12, 2023
1 parent a584b0f commit cd66289
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-domain-expert` will be documented in this file

## v1.2.0 - 2023-04-12

- auto scanning of routes

## v1.1.0 - 2023-04-12

- auto scanning of views
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ Domains

This structure helps you organize your code in a domain-driven manner, making it easier to manage and maintain as your application grows.

## Automatic View Scanning
## Auto-loading Routes and Views

Laravel Domain Expert can automatically scan and load view files in the appropriate domain without any configuration required.
The package includes built-in support for automatically loading routes and views for each domain. When your package is installed and the service provider is registered, the DomainAutoScanServiceProvider class will scan the Domains directory and automatically discover and load the route and view files for each domain.

### Example: Calling views in controllers
To reference a view within a domain, use the domain name as the namespace, followed by two colons and the view file path. Here's an example of how to call a view in a controller:

```php
return view('DomainName::view-name');
```

### Testing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Salehhashemi\LaravelDomainExpert;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class DomainViewServiceProvider extends ServiceProvider
class DomainAutoScanServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
Expand All @@ -24,10 +25,28 @@ public function boot()
$domainName = basename($domainPath);

$viewsPath = $domainPath.'/resources/views';
$routesPath = $domainPath.'/routes';

if (File::exists($viewsPath)) {
$this->loadViewsFrom($viewsPath, $domainName);
}

if (File::exists($routesPath)) {
$this->loadRoutes($domainName, $routesPath);
}
}
}

/**
* Load the routes for the domain.
*/
protected function loadRoutes(string $domainName, string $routesPath): void
{
$routeFiles = File::allFiles($routesPath);

foreach ($routeFiles as $routeFile) {
Route::namespace("App\\Domains\\{$domainName}\\Http\\Controllers")
->group($routeFile->getPathname());
}
}
}
2 changes: 1 addition & 1 deletion src/LaravelDomainExpertServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LaravelDomainExpertServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->app->register(DomainViewServiceProvider::class);
$this->app->register(DomainAutoScanServiceProvider::class);
}

/**
Expand Down

0 comments on commit cd66289

Please sign in to comment.