Commit e74a6ce9fa058632531757d96f99af05a5bd67be

Authored by Андрей Ларионов
1 parent 5b2dcf44bc

Раздел SEO в админке

Showing 7 changed files with 173 additions and 2 deletions Side-by-side Diff

app/Http/Controllers/Admin/CompanyController.php
... ... @@ -2,6 +2,7 @@
2 2  
3 3 namespace App\Http\Controllers\Admin;
4 4  
  5 +use App\Classes\Meta;
5 6 use App\Http\Controllers\Controller;
6 7 use App\Http\Requests\PagesRequest;
7 8 use App\Http\Requests\SEORequest;
... ... @@ -79,6 +80,23 @@ class CompanyController extends Controller
79 80 return redirect()->route('admin.editor-seo');
80 81 }
81 82  
  83 + public function editor_seo_ajax(Request $request) {
  84 + $url = $request->get('url'); // post('url');
  85 + $metaData = Array();
  86 + //$url = json_decode($url, true);
  87 +
  88 + if (!empty($url)) {
  89 +
  90 + $meta = new Meta($url);
  91 + $meta->parse();
  92 + $metaData = $meta->finalize();
  93 +
  94 + return json_encode($metaData);
  95 + } else {
  96 + return json_encode(Array('Error URL'));
  97 + }
  98 + }
  99 +
82 100 public function editor_seo_edit(SEO $page) {
83 101 return view('admin.seo.edit', compact('page'));
84 102 }
app/Http/Requests/SEORequest.php
... ... @@ -13,7 +13,7 @@ class SEORequest extends FormRequest
13 13 */
14 14 public function authorize()
15 15 {
16   - return false;
  16 + return true;
17 17 }
18 18  
19 19 /**
... ... @@ -24,7 +24,38 @@ class SEORequest extends FormRequest
24 24 public function rules()
25 25 {
26 26 return [
27   - //
  27 + 'title' => [
  28 + 'required',
  29 + 'string',
  30 + 'min:3',
  31 + 'max:255',
  32 + ],
  33 + 'url' => [
  34 + 'required',
  35 + 'string',
  36 + 'min: 5',
  37 + 'max:2555',
  38 + ],
  39 + ];
  40 + }
  41 +
  42 + public function messages() {
  43 + return [
  44 + 'required' => 'Поле :attribute обязательно для ввода',
  45 + 'unique' => 'Поле :attribute должно быть уникальным',
  46 + 'mimes' => 'Допускаются файлы только с расширением jpeg,jpg,png',
  47 + 'min' => [
  48 + 'string' => 'Поле «:attribute» должно быть не меньше :min символов',
  49 + 'integer' => 'Поле «:attribute» должно быть :min или больше',
  50 + 'file' => 'Файл «:attribute» должен быть не меньше :min Кбайт'
  51 + ],
  52 +
  53 + 'max' => [
  54 + 'string' => 'Поле «:attribute» должно быть не больше :max символов',
  55 + 'integer' => 'Поле «:attribute» должно быть :max или меньше',
  56 + 'file' => 'Файл «:attribute» должен быть не больше :max Кбайт'
  57 + ],
  58 +
28 59 ];
29 60 }
30 61 }
... ... @@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model;
8 8 class SEO extends Model
9 9 {
10 10 use HasFactory;
  11 +
  12 + protected $fillable = [
  13 + 'url',
  14 + 'title',
  15 + 'description',
  16 + 'keywords',
  17 + 'name', ];
11 18 }
resources/views/admin/seo/add.blade.php
... ... @@ -0,0 +1,7 @@
  1 +@extends('layout.admin', ['title' => 'Админка - Добавление SEO страницы'])
  2 +
  3 +@section('content')
  4 + <form method="POST" action="{{ route('admin.add-seo-store') }}">
  5 + @include('admin.seo.form')
  6 + </form>
  7 +@endsection
resources/views/admin/seo/edit.blade.php
... ... @@ -0,0 +1,7 @@
  1 +@extends('layout.admin', ['title' => 'Админка - Редактирование SEO страницы'])
  2 +
  3 +@section('content')
  4 + <form method="POST" action="{{ route('admin.update-seo', ['page' => $page->id]) }}">
  5 + @include('admin.seo.form')
  6 + </form>
  7 +@endsection
resources/views/admin/seo/form.blade.php
... ... @@ -0,0 +1,100 @@
  1 +@csrf
  2 +
  3 +@isset($page)
  4 + @method('PUT')
  5 +@endisset
  6 +<script>
  7 +window.onload = function() {
  8 + const MetaURLBtn = document.querySelector('#GetMetaData');
  9 +
  10 + MetaURLBtn.addEventListener('click', (e) => {
  11 + e.preventDefault();
  12 + console.log('Click button for get info metadata');
  13 +
  14 + let url = document.querySelector('#url').value;
  15 +
  16 + $.ajax({
  17 + type: "GET",
  18 + url: "{{ route('admin.ajax.seo') }}",
  19 + data: "url=" + url,
  20 + success: function (data) {
  21 + console.log('URL был передан ');
  22 + data = JSON.parse(data);
  23 + console.log(data);
  24 +
  25 + document.querySelector('#title').value = data['title'];
  26 + document.querySelector('#keywords').value = data['keywords'];
  27 + document.querySelector('#description').value = data['description'];
  28 + },
  29 + headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
  30 + error: function (data) {
  31 + console.log('Error: ' + data);
  32 + }
  33 + });
  34 +
  35 + });
  36 +}
  37 +
  38 +</script>
  39 +<div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
  40 +
  41 + <label class="block text-sm">
  42 + <span class="text-gray-700 dark:text-gray-400">URL страницы</span>
  43 + <input name="url" id="url"
  44 + class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
  45 + placeholder="URL страницы" value="{{ old('url') ?? $page->url ?? '' }}"
  46 + />
  47 + @error('url')
  48 + <span class="text-xs text-red-600 dark:text-red-400">
  49 + {{ $message }}
  50 + </span>
  51 + @enderror
  52 + </label>
  53 + <div style="margin-top: 10px;">
  54 + <a id="GetMetaData" class="px-3 py-1 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-md active:bg-purple-600 hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple" style="margin-top:10px;">Получить мета-данные</a><br><br>
  55 + </div>
  56 + <label class="block text-sm">
  57 + <span class="text-gray-700 dark:text-gray-400">Заголовок страницы (meta title)</span>
  58 + <input name="title" id="title"
  59 + class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
  60 + placeholder="Заголовок страницы" value="{{ old('title') ?? $page->title ?? '' }}"
  61 + />
  62 + @error('title')
  63 + <span class="text-xs text-red-600 dark:text-red-400">
  64 + {{ $message }}
  65 + </span>
  66 + @enderror
  67 + </label><br>
  68 +
  69 + <label class="block text-sm">
  70 + <span class="text-gray-700 dark:text-gray-400">Описание (meta description)</span>
  71 + <textarea class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
  72 + name="description" id="description" placeholder="Описание" required
  73 + rows="3">{{ old('description') ?? $page->description ?? '' }}</textarea>
  74 + @error('description')
  75 + <span class="text-xs text-red-600 dark:text-red-400">
  76 + {{ $message }}
  77 + </span>
  78 + @enderror
  79 + </label><br>
  80 +
  81 + <label class="block text-sm">
  82 + <span class="text-gray-700 dark:text-gray-400">Ключевые слова (meta keywords)</span>
  83 + <textarea class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
  84 + name="keywords" id="keywords" placeholder="Описание" required
  85 + rows="3">{{ old('keywords') ?? $page->keywords ?? '' }}</textarea>
  86 + @error('keywords')
  87 + <span class="text-xs text-red-600 dark:text-red-400">
  88 + {{ $message }}
  89 + </span>
  90 + @enderror
  91 + </label><br>
  92 +
  93 + <div class="flex flex-col flex-wrap mb-4 space-y-4 md:flex-row md:items-end md:space-x-4">
  94 + <div>
  95 + <button type="submit" class="px-3 py-1 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-md active:bg-purple-600 hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple">
  96 + Сохранить
  97 + </button>
  98 + </div>
  99 + </div>
  100 +</div>
... ... @@ -184,6 +184,7 @@ Route::group([
184 184 Route::get('editor-seo', [CompanyController::class, 'editor_seo'])->name('editor-seo');
185 185 Route::get('editor-seo/add', [CompanyController::class, 'editor_seo_add'])->name('add-seo');
186 186 Route::post('editor-seo/add', [CompanyController::class, 'editor_seo_store'])->name('add-seo-store');
  187 + Route::get('editor-seo/ajax', [CompanyController::class, 'editor_seo_ajax'])->name('ajax.seo');
187 188 Route::get('editor-seo/edit/{page}', [CompanyController::class, 'editor_seo_edit'])->name('edit-seo');
188 189 Route::put('editor-seo/edit/{page}', [CompanyController::class, 'editor_seo_update'])->name('update-seo');
189 190 Route::delete('editor-seo/delete/{page}', [CompanyController::class, 'editor_seo_destroy'])->name('delete-seo');