akio
akio

Reputation: 1021

Backbone and slim framework redirect route htacces

I'm trying to configure htaccess for my backbone and slim Webapp.

Here is my root url: mysite.com/myproject/

Here is how I'm organizing my directories:

directories

I would like do something like mysite.com/myproject/posts to get full list of my posts or mysite.com/myproject/post/:id to get one post

I know that I can use redirect to "simulate" URLs that do not exist. In theory if I try mysite.com/posts, Apache/.htaccess redirect to server.php where my "routes" do what I want and return my result to backbone.

But whatever I've tried, does seem to work.

EDIT

I've find part of solution.

I moved htaccess and server.php files into data directorie.

Here is my server.php file

<?php
    require_once("Slim/Slim.php");
    $app = new Slim();

    $app->get('/posts','getPosts');
    $app->get('/post/:id','getPost');

    $app->run();

    function getPosts() { echo "Hello world, get post list"; }
    function getPost($id) { echo "Hello world, get one post = ".$id; }
?>

Here is my backbone collection declaration

window.PostList = Backbone.Collection.extend({
        model : Post,
        url:"data/posts",

        initialize : function() {
            console.log ("collection created.")
        }
    });

And .htaccess file

RewriteEngine On
RewriteBase /myproject/data
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ server.php [QSA,L]

On fetch method of backbone, request was sent to http://mysite.com/myproject/data/posts and i can see response into console Hello world, get post list

but i can't do request like mysite/myproject/posts which value for RewriteBase ? which value for backbone collection ?

Upvotes: 0

Views: 680

Answers (1)

tkone
tkone

Reputation: 22728

What it sounds like you need to do is create a rewrite rule for mysite.com/posts instead of a 301 or 302 redirect.

So you'd have your server rewrite mysite.com/posts to server.php. This way /posts is still the URL.

Then you'd initialize your Backbone router to start and know how to handle /posts

var my_router = new (Backbone.Router.extend())();
my_router.route('/path/:id', show_post);
Backbone.history.start({pushState: true, root: '/});

Or something similar.

The trick being that the URL in your browser needs to match what Backbone is being told the route is. After that routes will no longer be called from your server, but you should support them if they have to reload or come in from scratch.

The best way to handle this is to actually make your rewrite rule something like:

RewriteRule ^/posts/(.*?)    server.php?post=$1 [L,R]

(Or something depending on how your server.php script works and how you want your URL structure to work).

Upvotes: 0

Related Questions