added api for dynamic page content
This commit is contained in:
parent
6eac5916d3
commit
f9347378ea
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Requests\PageRequest;
|
||||
use Backpack\CRUD\app\Http\Controllers\CrudController;
|
||||
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
|
||||
|
||||
/**
|
||||
* Class PageCrudController
|
||||
* @package App\Http\Controllers\Admin
|
||||
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
|
||||
*/
|
||||
class PageCrudController extends CrudController
|
||||
{
|
||||
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
|
||||
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
|
||||
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
|
||||
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
|
||||
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
|
||||
|
||||
/**
|
||||
* Configure the CrudPanel object. Apply settings to all operations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setup()
|
||||
{
|
||||
CRUD::setModel(\App\Models\Page::class);
|
||||
CRUD::setRoute(config('backpack.base.route_prefix') . '/page');
|
||||
CRUD::setEntityNameStrings('page', 'pages');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define what happens when the List operation is loaded.
|
||||
*
|
||||
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
|
||||
* @return void
|
||||
*/
|
||||
protected function setupListOperation()
|
||||
{
|
||||
CRUD::column('title');
|
||||
CRUD::column('content');
|
||||
|
||||
/**
|
||||
* Columns can be defined using the fluent syntax or array syntax:
|
||||
* - CRUD::column('price')->type('number');
|
||||
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Define what happens when the Create operation is loaded.
|
||||
*
|
||||
* @see https://backpackforlaravel.com/docs/crud-operation-create
|
||||
* @return void
|
||||
*/
|
||||
protected function setupCreateOperation()
|
||||
{
|
||||
CRUD::setValidation(PageRequest::class);
|
||||
|
||||
CRUD::field('title');
|
||||
CRUD::field('content')->type('ckeditor');
|
||||
|
||||
/**
|
||||
* Fields can be defined using the fluent syntax or array syntax:
|
||||
* - CRUD::field('price')->type('number');
|
||||
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Define what happens when the Update operation is loaded.
|
||||
*
|
||||
* @see https://backpackforlaravel.com/docs/crud-operation-update
|
||||
* @return void
|
||||
*/
|
||||
protected function setupUpdateOperation()
|
||||
{
|
||||
$this->setupCreateOperation();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,10 @@ class MultimediaController extends ApiController
|
|||
{
|
||||
public function index($category)
|
||||
{
|
||||
$medias = MultimediaCategory::where('id', $category)->with('medias')->get()->first()->medias;
|
||||
return $this->respondWithCollection($medias, new MediaTransformer());
|
||||
$medias = MultimediaCategory::where('id', $category)->with('medias')->get()->first();
|
||||
if($medias){
|
||||
return $this->respondWithCollection($medias->medias, new MediaTransformer());
|
||||
}
|
||||
return $this->errorNotFound();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Transformers\PageTransformer;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PageController extends ApiController
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$page_title = $request->page;
|
||||
if(!$page_title){
|
||||
return $this->errorWrongArgs();
|
||||
}
|
||||
$page = Page::where('title', $page_title)->get()->first();
|
||||
if($page){
|
||||
return $this->respondWithItem($page, new PageTransformer($this->locale));
|
||||
}
|
||||
return $this->errorNotFound();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class PageRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
// only allow updates if the user is logged in
|
||||
return backpack_auth()->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// 'name' => 'required|min:5|max:255'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation attributes that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation messages that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StorePageRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdatePageRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
|
||||
|
||||
class Page extends Model
|
||||
{
|
||||
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
|
||||
use HasFactory;
|
||||
use HasTranslations;
|
||||
|
||||
protected $guarded = [''];
|
||||
public $translatable = ['content'];
|
||||
}
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class PagePolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
/**
|
||||
* Determine whether the user can view any models.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function viewAny(User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view the model.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Page $page
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function view(User $user, Page $page)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create models.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function create(User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the model.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Page $page
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function update(User $user, Page $page)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the model.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Page $page
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function delete(User $user, Page $page)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the model.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Page $page
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function restore(User $user, Page $page)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the model.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Page $page
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
*/
|
||||
public function forceDelete(User $user, Page $page)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Transformers;
|
||||
|
||||
use App\Models\Page;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class PageTransformer extends TransformerAbstract
|
||||
{
|
||||
private $locale;
|
||||
|
||||
public function __construct($locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function transform(Page $page)
|
||||
{
|
||||
return [
|
||||
'id' => $page->id,
|
||||
'title' => $page->title,
|
||||
'content' => $page->getTranslations('content', [$this->locale])[$this->locale] ?? '-',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class PageFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePagesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('pages', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title')->unique();
|
||||
$table->longText('content');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('pages');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class PageSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
|
@ -20,4 +20,5 @@
|
|||
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('tarif') }}"><i class="nav-icon la la-question"></i> Tarifs</a></li>
|
||||
|
||||
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('contact') }}"><i class="nav-icon la la-question"></i> Contacts</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('video') }}"><i class="nav-icon la la-question"></i> Videos</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('video') }}"><i class="nav-icon la la-question"></i> Videos</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('page') }}"><i class="nav-icon la la-question"></i> Pages</a></li>
|
||||
|
|
@ -12,6 +12,7 @@ use App\Http\Controllers\Api\DocumentController;
|
|||
use App\Http\Controllers\Api\TarifController;
|
||||
use App\Http\Controllers\Api\MultimediaCategoryController;
|
||||
use App\Http\Controllers\Api\MultimediaController;
|
||||
use App\Http\Controllers\Api\PageController;
|
||||
use App\Http\Controllers\Api\VideoController;
|
||||
|
||||
/*
|
||||
|
|
@ -45,4 +46,5 @@ Route::get('tariffs', [TarifController::class, 'index']);
|
|||
Route::get('contacts', [ContactController::class, 'index']);
|
||||
|
||||
Route::get('video', [VideoController::class, 'index']);
|
||||
Route::get('pages', [PageController::class, 'index']);
|
||||
|
||||
|
|
|
|||
|
|
@ -27,4 +27,5 @@ Route::group([
|
|||
Route::crud('multimedia', 'MultimediaCrudController');
|
||||
Route::crud('contact', 'ContactCrudController');
|
||||
Route::crud('video', 'VideoCrudController');
|
||||
Route::crud('page', 'PageCrudController');
|
||||
}); // this should be the absolute last line of this file
|
||||
Loading…
Reference in New Issue