Hacking public search into Wardrobe CMS

Wardrobe CMS doesn't come with search built in (at least as far as I could tell) so I've hacked it in.

I did think about using MySQL's inbuilt full-text matching thing, but I didn't really want to mess with the database structure. So I decided to use a package that I've used in other applications, Searchable.

Unfortunately, if you do composer require nicolaslopezj/searchable:1.* it will throw an error because WardrobeCMS is built on Laravel 4.1. Since the repository is only one file (SearchableTrait), we can copy and paste it over into our project. I've created a folder under app called Blog and added it to my composer.json file autoloader. You'll also need to copy over vendor\wardrobe\core\src\Wardrobe\Core\Models\Post.php to that folder as well (updating the namespace) as we'll be making a slight modification to the posts model; we don't want to edit anything under the vendor folder.

Follow the steps on the GitHub page to your Post.php model in your Blog folder. Once you're done, you're ready to implement the search.

Under your theme routes.php, add the desired route, so you get something like this:

<?php
Route::get('/search/{q?}', function ($q = null) {
    if (Input::has('q')) {
        return Redirect::to(url('search/' . Input::get('q')));
    }
    if ($q) {
        $results = \App\Blog\Models\Post::search($q)->paginate(Config::get('core::wardrobe.per_page'));
        if (count($results) == 0) {
            $results = [];
        }
        return View::make(theme_view('search'))->with('results', $results);
    }

    return View::make(theme_view('search'));
});

This most definitely is not the best way to do this, but I wanted a quick and dirty way of doing this.