Botnary / wp-slim-framework

Slim framework as Wordpress plugin
73 stars 26 forks source link

Help starting #1

Closed KenEucker closed 9 years ago

KenEucker commented 9 years ago

Hello, I am looking to use Slim with wordpress and another custom plugin I am developing and I am using this plugin you developed here but I have a couple of questions:

Where does /slim/api actually exist that the api routes live on? If my wordpress site is at http://localhost/site/wp/ should I change the setting in the settings page for Slim Framework? Or do I try accessing it at http://localhost/site/wp/slim/api which I have tried but does not work.

Also, where are you putting the code that calls the action for slim mapping? I put my code in my custom plugin and I just want to make sure it is being called with the correct timing.

Thank you in advance for your assistance!

Botnary commented 9 years ago

I did not try it with Wordpress installed in a subfolder. I think you need to go in settings /wp-admin/options-general.php?page=slim_framework and add site/wp/slim/api for Base Path. And after your REST calls will look like /site/wp/slim/api/books/1

You can put that action like any other action in Wordpress, functions.php, plugin.php, etc... also is good to have it called before any content is sent to the browser.

KenEucker commented 9 years ago

Thanks for the quick response!

I changed the setting but I'm still not seeing my added route response. I just get the wordpress "It looks like nothing was found at this location. Maybe try a search?" page.

The way I had slim working outside of wordpress was by requesting the page I was on and it would catch the routes like so: site/rest.php/put

Inside wordpress I am defining the routes in a class I have as a member of my plugin class that I instantiate by passing the $slim object after a callback in my plugin class is fired from the action 'slim_mapiing'. Am I doing that part correctly at least?

Botnary commented 9 years ago

Is your wordpress using permalinks ?

KenEucker commented 9 years ago

Yes and I changed from the default to date and name.

Botnary commented 9 years ago

in your plugin.php you should have something like this.

/*
Plugin Name: Your plugin name
Version: 1.0.1
*/
add_action('slim_mapping',function(Slim\Slim $app){
    //here you have access to slim $app and can define routes
    $app->get('/site/wp/slim/api/books/:id',function($bookId)use($app){
        //do something with this book 
    });
});
if like this still does not work i will try it by myself in a wordpress installed in a subfolder.
KenEucker commented 9 years ago

I added that line to my plugin and requesting /site/wp/slim/api/books/1 returns a 404. The 404 looks like one that slim spits out, so that's hopeful. I just don't think the routes are working.

Thanks again!

Botnary commented 9 years ago

If you see a slim 404 then it's a good sign. I will try on my side with a wordpress in a sub folder.

Botnary commented 9 years ago

Alright after some tests today, for it to work with a Wordpress installed in a sub folder you will need to use it like in this example. My wordpress is installed in /wp/ folder. In the settings /wp-admin/options-general.php?page=slim_framework i added /wp/slim/api/ and saved, and my permalinks saved to use http://localhost.com/wp/%postname%/ Then i run this tests:

/**
 * Plugin Name: Test Wordpress Slim framework
 */
//example outside a class
add_action('slim_mapping',function($app){
    //notice the path is without "wp"
     $app->get('/slim/api/books',function(){
        echo 'get your books';
    });
});

class TestPlugin{

    function __construct()
    {
        add_action('slim_mapping',array($this,'routes'));
    }

    function routes($app){
        $self = $this;
        //notice the path is without "wp" 
       $app->get('/slim/api/students',function()use($self){
            $self->listStudents();
        });
    }

    function listStudents(){
        echo 'i just listed all students :)';
    }
}
//initialize the plugin
new TestPlugin();

And everything worked as expected. Also the calls i did over ajax like :

//then here we use '/wp/' because we need to tell Apache where Wordpress is with our plugin.
jQuery.get('/wp/slim/api/students')
jQuery.get('/wp/slim/api/books')

And it worked. I hope this was helpful :)

KenEucker commented 9 years ago

That worked! Thanks!

To recap:

My issue had to do with the base url not being setup correctly. I had my wordpress installation within another site on localhost, therefore my path from the hostname (http://localhost/) needed to be corrected. In the Slim Framework settings I changed it from "/wp/slim/api/" to "/mt/wp/slim/api" where my site name on localhost was "mt".

Furthermore, the routes needed the proper base url plugged into them from the sitename, (http://localhost/mt/), of "/wp/slim/api".

Finally, when requesting the routes we need to use the right path from where we are to /slim/api. If we are within wordpress this request, I assume, will be "/slim/api" but if we are calling it from outside the wordpress environment it will be "/wp/slim/api".

If I end up making changes to your source to make this setup easier than I will send you a pull request. Thank you so much for your help and I look forward to using your code! :+1:

Botnary commented 9 years ago

No problem, glad it worked for you.