PHP code example of benaaacademy / platform

1. Go to this page and download the library: Download benaaacademy/platform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

benaaacademy / platform example snippets



	return [

    	/**
     	* | Admin prefix (the admin url segment)
     	* |
     	* | @var string
     	*/

    	'prefix' => env("ADMIN_PREFIX", "backend"),

    	/**
     	* | Default URI after user authentication
     	* | without admin prefix
     	* | @var string
     	*/

    	'default_path' => env("DEFAULT_PATH", "posts"),


### Plugin structure:

Plugins folder is located at root directory besides app folder.

Each plugin may contain these directories:

- `config`
- `controllers`
- `lang`
- `models`
- `migrations`
- `views`
- `routes.php`
- `{Name}Plugin.php` 

### Creating plugin
	
	php artisan plugin:make plugin-name --plain

you can also create a plugin with some extra resources.

	php artisan plugin:make plugin-name --resources
	
	
By default the plugin is uninstalled. To install Run: 

	php artisan plugin:install plugin-name
	


###Plugin bootstrap file class

Naming this file will be the upper camel case of plugin folder suffixed with Plugin.php

Example:

 - The plugin file with folder name "products" will be ProductsPlugin.php and the plugin file with folder name "featured_products" will be FeaturedProductsPlugin.php.

The plugin Class name is the same as plugin file name.


####A sample "products" plugin

 
 	

	/*
 	 * Class ProductsPlugin
 	 */
 	
	class ProductsPlugin extends Plugin {

    	/** [optional]
     	* | Plugin Service providers
     	* |
     	* | @var array
     	*/
    	public $providers = [];

    	/** [optional]
     	* | Plugin aliases
     	* |
     	* | @var array
     	*/
    	public $aliases = [];

    	/** [optional]
     	* | Admin commands
     	* | Located in plugin commands folder
     	* | @var array
     	*/
    	public $commands = [];

    	/** [optional]
    	* | plugin middlewares are located in plugin middlewares folder
     	* | @var array
     	*/
    	public $middlewares = [
    		//PluginMiddleware::class
    	];

       /** [optional]
     	* | Plugin routed middleware.
     	* | Located in plugin middlewares folder
     	* | These middleware may be assigned to groups or used individually.
     	* |
     	* | @var array
     	*/
    	public $route_middlewares = [];

    	/** [optional]
     	* | Plugin permissions
     	* | A list of plugin permissions
     	* | @var array
     	*/
    	public $permissions = [
    		// "create"
    	];


    	/** [
     * @var array
     */
    protected $creatingRules = [
		'title' => '

	
	 /*
	 * Adding some custom messages to validator
     * @return array
     */
    protected function setValidationMessages()
    {
        return [
        	"name.s()
    {
        return [
        	"name" => "Client name",
        	// and more
        ]
    }
	
     /**
     * @param $v
     * @return mixed
     * run when creating or updating a model
     */
    function setValidation($v)
    {

        $v->sometimes("content", "ixed
     * run when updating a model
     */
    function setUpdateValidation($v)
    {

        $v->sometimes("content", "    // A unique slug will be created and stored without assigning it.
     
 
###Migrations directory:

You can create a new plugin migration file using

	php artisan plugin:migration {migration_file_name} {module_name}

And run plugin migration

	php artisan plugin:migrate {module_name}
	
	
###Views directory:

View are accessed from anywhere using 

	return view({module_name}::{view_file_name}); 

###Lang directory:

You can access plugin lang item using:

	trans("{module_name}::{lang_file}.{lang_key}");
	
Some extra keys are created to translate module name, permissions, attributes and custom validation messages

	
	
	'module' => '{module_title}',
	
	'attributes' => [
	    
	    'title' => 'Title',
	    
	    // some other fields
	],

	'permissions' => [
	    
	    'create_item' => 'Create an item',
	    
	    // some other messages
	],
	
	'messages' => [
	    
	    '

	
	Route::group([
    
    	"prefix" => ADMIN,
    
    	"middleware" => ["web", "auth"],
	
	], function ($route) {
	
			// routes goes here
			
	});



### Actions:

- Actions are the hooks that the cms core launches at specific points during execution, or when specific events occur. Plugins can specify that one or more of its PHP functions are executed at these points, using the Action API.

- Actions are built on [laravel events](https://laravel.com/docs/5.2/events).

---

#####Example:
	
	
	
	// Using callbacks
	Action::listen('auth.login', function($user){
		// do some tasks
	});
	
	// Or call `onLogin` method in `LoginHandler` class
	Action::listen('auth.login', 'LoginHandler@onLogin');
	
	// Or call `handle` method in `LoginHandler` class
	Action::listen('auth.login', 'LoginHandler');
	
	// Adding Action priority if there are more than one listener
	Action::listen('auth.login', 'LoginHandler', 1);


Developers may need to add some fields to admin forms.
Let's do an example with actions.

#####Example: Adding gender field to users:

We will add this code to plugin class boot method to add the the html view of field using `user.form.featured` action.

	
	
	Action::listen("user.form.featured", function ($user) {
    
    	return view("users::custom")->with("user", $user);
    	
	});

In `custom` view, we will add the