Skip to content

Instantly share code, notes, and snippets.

@ArnaudValensi
Last active April 27, 2018 10:18
Show Gist options
  • Save ArnaudValensi/3a5e50bc50a214f631e8e6e25aedd881 to your computer and use it in GitHub Desktop.
Save ArnaudValensi/3a5e50bc50a214f631e8e6e25aedd881 to your computer and use it in GitHub Desktop.
Example of a Forest Smart Action on Laravel 5.5.x+
<!-- config/forest.php -->
<?php
return [
'secret_key' => '7a3049f8cd14e719c219e6d75e2a7715eef38bf6ec4be2ffc774a3374c9196a8',
'auth_key' => 'l8oeFltNrdrbq6pPeEGzxbG47h2qCK46',
'models_path' => 'app',
'url' => 'http://localhost:3001',
'debug_mode' => true,
'actions' => [
'AppUser' => [
['name' => 'ban']
]
]
];
<!-- app/Providers/RouteServiceProvider.php -->
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
// Add this
$this->mapForestRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
// Add this
protected function mapForestRoutes()
{
Route::prefix('forest/actions')
->middleware('auth.forest')
->namespace($this->namespace)
->group(base_path('routes/forest.php'));
}
}
<!-- routes/forest.php -->
<?php
Route::post('ban', 'UsersController@ban');
<!-- app/Http/Controllers/UsersController.php -->
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response as Response;
use ForestAdmin\ForestLaravel\Logger;
class UsersController extends Controller {
public function ban(Request $request) {
Logger::debug('UsersController@ban');
// $id = $request->input('data')['attributes']['ids'][0];
// $user = User::find($id);
// $user->banned = true;
// $user->save();
return Response::make('OK', 204);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment