Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Created January 9, 2015 23:11
Show Gist options
  • Save tommymarshall/ee41e5c49c39420a7957 to your computer and use it in GitHub Desktop.
Save tommymarshall/ee41e5c49c39420a7957 to your computer and use it in GitHub Desktop.
Themosis Routes
<?php
// Home
Route::get('home', 'PagesController@home');
// @about method always fires when viewing a page, no matter the page
Route::any('page' , [['about'], 'uses' => 'PagesController@about']);
Route::any('page' , [['vision'], 'uses' => 'PagesController@vision']);
Route::any('archive' , ['blog', 'uses' => 'BlogController@index']);
@jlambe
Copy link

jlambe commented Jan 10, 2015

Hi,

I've tested your code on a fresh installation and everything is working correctly.

Inside the page routes, when you specify the ['about'] or ['vision'] argument, you're basically listening to a request made to the about or vision page URI:

  • //somedomain.tld/about/
  • //somedomain.tld/vision/

In order to make this to work, you need to change the permalink structure to anything except default.

If you keep the default WordPress permalink, only ID arguments are working for requests. Let me know how it goes.

@tommymarshall
Copy link
Author

Context:

  1. I am using Laravel's Homestead environment.
  2. I have created both an About and Vision page.
  3. I have my permalink structure set to Post name. (Tried others)

When I visit //domain.tld/vision/ it triggers the about route (PagesController@about). When I swap lines 7 and 8, then all page requests trigger the vision route. So, whichever route is defined first is what gets triggered.

This must have something to do with the fact I'm using Homestead? :/

@jlambe
Copy link

jlambe commented Jan 12, 2015

I think the issue is coming because Homestead is running Nginx. Normally WordPress runs on Apache and when you set/change the permalink structure, the scripts update the .htaccess file so it can work.

But with nginx, there is no .htaccess file so the requests can't work properly.

I also use nginx and it works as expected. In order to make WordPress work with nginx, you have to define the following statement inside your server bloc:

location / {
    try_files $uri $uri/ /index.php?$args;
}

I have a version of the framework running on nginx and PHP 5.6 and everything is working correctly. I suspect the configuration of Homestead nginx is not suitable to WordPress.

Also, try perhaps with default permalink and visit the different pages using those uri ?page_id=8 or simply set the default permalink and on a page edit screen click view page. Normally it should work as expected without the pretty links. Let me know how it goes.

@tommymarshall
Copy link
Author

Made that change to nginx conf, still same. Here's a gif of what's happening.

Gif

@jlambe
Copy link

jlambe commented Jan 12, 2015

hmm...that's weird. I suppose I'll have to test it using homestead and find out why this doesn't work. I'll try this week the homestead/themosis installation and come back to you if I find something.

The only suggestion I can give you right now is to try to use another vagrant file loading a basic Lemp environment and let me know how it goes. In theory it should work correctly on nginx but I must admit that it's not yet heavily tested. I'll post this issue on Twitter also so perhaps someone with heavy knowledge of homestead/nginx/WordPress could help 😄

@tommymarshall
Copy link
Author

So, this is interesting, got it working using this syntax:

<?php

Route::get('page' , ['about', 'uses' => 'PagesController@about']);
Route::get('page' , ['vision', 'uses' => 'PagesController@vision']);

Basically instead of my pages ("about" and "vision") being passed as an array, just passing them as a string works. This if fine for me since I am not using ID's and have permalinks set up.

EDIT: Ah, just saw this is documented in "Other way to use a Controller" section at http://framework.themosis.com/docs/controllers/

The new issue appears to be that POST requests are not being matched, even though I am using Form::open(). Hmmm...

@jlambe
Copy link

jlambe commented Jan 12, 2015

Update: I made a mistake. I've tested again and in fact there is an issue when using an array in place of a string. This needs a patch as soon as possible.

Regarding the form, I can't see any issues. When listening to a POST request, the methods is triggered correctly. Don't forget to add a Route::post() otherwise this won't work.

I'm going to patch the framework this week so you'll be able to update it. Thanks for catching this one.

@jlambe
Copy link

jlambe commented Jan 13, 2015

Hi tommy,

can you apply this "patch" on the themosis-framework Route.php class located: themosis-framework/src/Themosis/Route/Route.php. Replace the lines 102->112 by the following code:

        if (!isset($action['params']))
        {
            // The first element passed in the action is used
            // for the WordPress conditional function parameters.
            $param = array_first($action, function($key, $value)
            {
                return is_string($value) || is_array($value);
            });

            if (is_string($param))
            {
                $action['params'] = (false !== strrpos($param, '@')) ? null : $action[0];
            }
            else if (is_array($param))
            {
                $action['params'] = $param;
            }
        }

I've also updated the master branch of the themosis-framework so if you want to test this, simply update your composer.json file to use the dev-master package and run a composer update.

@jlambe
Copy link

jlambe commented Jan 13, 2015

Let me know how it goes. I will test this patch on several applications. If everything is ok, I will release a minor version of the framework so this can be fixed.

Regarding the POST requests, let me know if you still get an error and otherwise, please paster your code so I can test it also.

@tommymarshall
Copy link
Author

Great! ran composer update and that did the trick. Thanks a bunch! The POST requests still sadly do not work. Here's the code: https://gist.github.com/tommymarshall/e90112157a8d76c6d4f1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment