first change
This commit is contained in:
0
Modules/Sitemap/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Sitemap/app/Http/Controllers/.gitkeep
Normal file
150
Modules/Sitemap/app/Http/Controllers/SitemapConfigController.php
Normal file
150
Modules/Sitemap/app/Http/Controllers/SitemapConfigController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Sitemap\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Sitemap\Interfaces\SitemapConfigInterface;
|
||||
use Modules\Sitemap\Models\SitemapConfig;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class SitemapConfigController extends Controller
|
||||
{
|
||||
|
||||
public function __construct(private SitemapConfigInterface $sitemapConfig)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$isEditing = $request->has("id");
|
||||
|
||||
$sitemapConfig = $isEditing ? $this->sitemapConfig->findById($request->id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
|
||||
$model = SitemapConfig::query()->latest();
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('status', function (SitemapConfig $sitemapConfig) {
|
||||
$status = $sitemapConfig->status ? 'Published' : 'Draft';
|
||||
$color = $sitemapConfig->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('action', 'sitemap::sitemapConfig.datatable.action')
|
||||
->rawColumns(['action', 'status'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('sitemap::sitemapConfig.index', [
|
||||
'sitemapConfig' => $sitemapConfig,
|
||||
'title' => $isEditing ? 'Edit Sitemap Config' : 'Add Sitemap Config',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$isEditing = $request->has('id');
|
||||
|
||||
if ($isEditing) {
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'max:255', 'unique:sitemap_configs,title,' . $request->id],
|
||||
'max_depth' => ['nullable', 'integer'],
|
||||
'max_crawl_count' => ['nullable', 'integer'],
|
||||
'sitemap_excludes' => ['nullable', 'string'],
|
||||
"ignore_robot" => ['required', 'boolean'],
|
||||
]);
|
||||
|
||||
$sitemapConfig = $this->sitemapConfig->update($request->id, $validated);
|
||||
session()->flash("success", "Sitemap Config successfully updated.");
|
||||
return to_route('sitemapConfig.index');
|
||||
}
|
||||
|
||||
$maxOrder = SitemapConfig::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'unique:sitemap_configs,title'],
|
||||
'max_depth' => ['nullable', 'integer'],
|
||||
'max_crawl_count' => ['nullable', 'integer'],
|
||||
'sitemap_excludes' => ['nullable', 'string'],
|
||||
"ignore_robot" => ['required', 'boolean'],
|
||||
'order' => ['integer'],
|
||||
]);
|
||||
|
||||
$sitemapConfig = $this->sitemapConfig->create($validated);
|
||||
session()->flash("success", "Sitemap Config successfully created.");
|
||||
|
||||
return to_route('sitemapConfig.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$sitemapConfig = $this->sitemapConfig->delete($id);
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'message' => "Sitemap Config successfully deleted."
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$sitemapConfig = $this->sitemapConfig->findById($id);
|
||||
|
||||
$sitemapConfig->update([
|
||||
'status' => !$sitemapConfig->status
|
||||
]);
|
||||
return response([
|
||||
'status' => 200,
|
||||
'message' => 'Sitemap Config successfully toggled.'
|
||||
], 200);
|
||||
}
|
||||
}
|
73
Modules/Sitemap/app/Http/Controllers/SitemapController.php
Normal file
73
Modules/Sitemap/app/Http/Controllers/SitemapController.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Sitemap\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Spatie\Crawler\Crawler;
|
||||
use Spatie\Sitemap\SitemapGenerator;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Modules\Sitemap\Models\SitemapConfig;
|
||||
|
||||
class SitemapController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$path = public_path('sitemap.xml');
|
||||
|
||||
if (!file_exists($path)) {
|
||||
return response('Sitemap not found.', 404);
|
||||
}
|
||||
|
||||
return response()->file($path, [
|
||||
'Content-Type' => 'application/xml',
|
||||
]);
|
||||
}
|
||||
|
||||
public function generate(Request $request)
|
||||
{
|
||||
$path = public_path('sitemap.xml');
|
||||
|
||||
$sitemapConfig = SitemapConfig::active()->latest()->first();
|
||||
$sitemapExcludes = $sitemapConfig->sitemap_excludes;
|
||||
$ignoreRobots = $sitemapConfig->ignore_robot;
|
||||
|
||||
if (is_string($sitemapExcludes)) {
|
||||
$sitemapExcludes = array_map('trim', explode(',', $sitemapExcludes));
|
||||
}
|
||||
|
||||
$generator = SitemapGenerator::create(config('app.url'))
|
||||
->configureCrawler(function (Crawler $crawler) use ($ignoreRobots, $sitemapConfig) {
|
||||
if ($ignoreRobots) {
|
||||
$crawler->ignoreRobots();
|
||||
}
|
||||
if ($sitemapConfig->max_depth) {
|
||||
$crawler->setMaximumDepth($sitemapConfig->max_depth);
|
||||
}
|
||||
})
|
||||
->shouldCrawl(function (UriInterface $url) use ($sitemapExcludes) {
|
||||
$path = $url->getPath();
|
||||
foreach ($sitemapExcludes as $exclude) {
|
||||
if (str_starts_with($path, $exclude)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if ($sitemapConfig->max_crawl_count) {
|
||||
$generator->setMaximumCrawlCount($sitemapConfig->max_crawl_count);
|
||||
}
|
||||
|
||||
$generator->writeToFile($path);
|
||||
|
||||
|
||||
if (!file_exists($path)) {
|
||||
return response('Failed to generate sitemap.', 404);
|
||||
}
|
||||
|
||||
return response()->file($path, [
|
||||
'Content-Type' => 'application/xml',
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user