themosis / documentation

Official documentation of the Themosis framework.
http://framework.themosis.com/
52 stars 37 forks source link

Create Plugin with page in twig in back office of Wordpress #46

Closed Givx closed 7 years ago

Givx commented 7 years ago

Hello,

I want to create a plugin with many page in the backend of wordpress. i want to reproduce a MVC structure and use twig view.

It's possible or not ?

Thanks

jlambe commented 7 years ago

Yes, and if you want to create admin pages, use the Page API: http://framework.themosis.com/docs/1.3/page/

You can't use the Route class for the admin. But instead you can define Actions with the load_$page hook and call a controller class from there with its method using this syntax Your\Class\Name@method, so you can perform logic before returning any content.

And as you can define a custom view for a page, you can also register a View Composer in order to send custom data to your page view and structure your code.

Givx commented 7 years ago

I create Page inside admin/plugin.php ??
Because i don't realy understand when i call http://monsite/wp-admin/admin.php?page=mon-plugin I have this all the time "Sorry, you are not allowed to access this page."

Givx commented 7 years ago

Sorry for all question, it's ok now. thanks a lot

jlambe commented 7 years ago

You can work within the admin directory or you can define Service providers, the choice is yours.

acermez commented 7 years ago

@jlambe First of all, thank you so much for the awesome framework! I have been using https://github.com/getherbert/herbert for the last couple months building great plugins but I decided to switch using Themosis Framework, so far so good for the front-end, but things got complicated when creating a plugin, usually I would create backend panels with Herbert and point the routing methods to a controller, I know you did answer @Givx question but I'm struggling here to point an admin page created with the Page API to a controller to manage normal CRUD views. Can you provide a short snippet of code of how to implement the Wordpress Settings hook to do that :) , thank you so much!

jlambe commented 7 years ago

@acermez Thanks 😃 Regarding the admin, WordPress do not use a front controller pattern so it's totally different in order to map an admin page request to a route and controller.

There are 2 possibilities:

  1. The best one is defining a custom view for your page and call a View Composer in order to work as the "controller".
  2. Use a "load_admin.php" hook, check your page query variable value and attach the hook callback to a class method - Works if you don't need to pass directly data to your custom page but perhaps verify that some options are set, ...
// Example 1 - View composer
View::composer('my.plugin.view', 'SomeController@method');
$view = View::make('my.plugin.view');
Page::make('slug', 'My Page', null, $view)->set();

// View composer...
// I think that if your class also extends the BaseController class, you might also
// get auto-resolving for declared dependencies as parameters...
public function method($view)
{
    // Work your logic here and then pass the data to your page view...
}

// Example 2 - Hook
// Inside your plugin resources/admin/actions.php file
use Themosis\Facades\Action;

Action::add('load_admin.php', 'MyControllerClass@load');

// Inside your plugin controller
// Use the constructor to auto-inject dependencies if needed
// then work from your load method. 
// From the givx example above, there is a page query var with a value
// of mon-plugin -> check this value before proceeding with extra logic
public function load()
{
    $page = Themosis\Facade\Input::get('page');
    if ('mon-plugin' !== $page) {
        return;
    }
    // Set your logic here. You can still define a View Composer call here in order to pass any data
   // to your custom admin page.
}
acermez commented 7 years ago

@jlambe Thank you so much for the quick reply ! I'm going with the view composer solution :) Meanwhile, is there any plans to implement an easier way to handle backend pages with simple routing like the front-end? I know that creating pages with Page::make will still be required but at least we can separate the POST, GET, etc... methods and map it to different methods of the controller. I know that Themosis Framework is way different than Herbert, but if we can point routes and shortcodes to a controller method, that will make Themosis Framework perfect :)
Thanks again for your support.

jlambe commented 7 years ago

@acermez Why not ;) Can you open a new issue for your feature request so I can track it later on ? Thanks

acermez commented 7 years ago

@jlambe The new issues has been created under the Themosis/Plugin repo.

For the "View Composer" solution, it is working great but still can't do what a controller method does, I mean yeah you can attach data to the view etc... but you can't for example redirect to another page after saving a model etc... ( this is why I think routing at backend is important :) ) Maybe there is a better way to do this that I don't know yet, but so far, I can't use Themosis Plugin for production and rapidly build plugin admin pages without having to use "WP" classic methods.

Again, thank you so much for the awesome Framework and your help!! I will keep an eye on the future releases.

jlambe commented 7 years ago

@acermez Perhaps you could help improve this. On your opened issue, can you document what you're trying to do (with code) so I can have a case scenario to work on when building a such feature. Thanks