Running my Backbone Render function on a selected route? - php

I have a PHP Slim and Backbone.JS setup and all my code is now working without any problems.
The only issue I have is that the code I have is minified into one file with Grunt.JS and is loaded at the bottom of each page.
So my Backbone render call is fired on all my pages within my site and not just the path I want it to run on.
I have now tried to use Backbones Router to fire the render on the path I want it to run on, I did not think this would work and it did not as I am using PHP slim as the routing agent and of course Backbone needs a /#/ route path.
Now when I had this Backbone route set up I did try to get PHP Slim to redirect the /#/ route to the clean PHP Slim route path. PHP Slim did not like this at all, when I use the following code,
$app->get('/#/MYPATHHERE', function () use ($app) {
$app->redirect('/REALLPATHTOGOTO');
});
it gave me a PHP Slim error, it looks like PHP Slim does not like the /#/ route.
So what is the best method for doing this?
I am thinking that I could just call the render function within the PHP page that I am getting PHP Slim to render on my selected route? or is there a better method for doing this?
Thanks
Glenn.

Ok got this to work with Backbone, did some more research and enabling pushState to true on the Backbone.history.start then it works without the need for the hash routing.

Related

PHP - What are PHP routing concepts I need to know to use routing libraries?

I'm trying to get simple routing running using Klein. I tried to run the simplest example:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$klein = new \Klein\Klein();
$klein->respond('GET', '/hello-world', function () {
return 'Hello World!';
});
$klein->dispatch();
When I go to localhost/mysitedomain/hello-world in my browser, I just get an HttpException: Fatal error: Uncaught Klein\Exceptions\HttpException in C:\xampp-portable-win32-7.2.9-0-VC15\xampp\htdocs\mywebsite\vendor\klein\klein\src\Klein\Exceptions\HttpException.php on line 36.
I read through the docs, and even other routing libraries but it seems like there's more to it than this. There's concepts like:
Matching
Responding
Rendering
Routing
Views
Controllers
Actions
I was just hoping to get a function callback called when I go to a certain route, but I don't know what the reason for the exception is. Do I need to match first? Then respond? Then render?
I've tried the dispatch library and while I don't get errors in that one, the route callback never gets called.
I have no idea how these routing libraries work. How do I get a simple working route with Klein?
The Klein.php router seems to be broken/outdated. Last commit was 3 years ago, so i don't think there will be any updates soon.
For beginners i can recommend using Slim Framework. It has more functions than routing, but you can just use the routing part and ignore the rest without any problems.

Integrating AngularJS in Symfony APP

Here i was i have :
A RESTful Symfony API that i created with few bundles
A front AngularJS that i have in the web repository
Now here is a sample of my routing file :
$routeProvider.
when('/liste-produits', {
templateUrl: '../templates/list-products.html',
controller: 'ProductListCtrl'
}).
otherwise({
redirectTo: '/'
});
The fact that i have to use "../". Because otherwise it won't work in dev environnement (app_dev.php). And of course by the time i will post it in production (app.php) i won't need to add this "../"
Do you guys understand my problem ?
Since i can get assetic from Symfony work in the routing file.
How can i solve this ?
There is an approach, where you define a global variable in your base twig file:
Symfony 2:image paths in javascript file with assetic which you can in turn use in e.g. AngularJS.
There is also a bundle called FOSJsRoutingBundle, it sort of exposes your routes to the client and thus javascript. That might be interesting for you.
However there is another option; - I have personally used the approach posted by Rein Baarsma with the twig file and then cached the resulting javascript.
It's fairly simple to write a request listener that renders the twig file to a javascript file once a day or whenever the javascript file is deleted.
I used the same approach with the stylesheets for a project with daily changing colors.
If you do not cache it, the browser will revisit the route returning the javascript on each page and rerender the javascript file, which adds a lot of overhead.
You could simply make a Symfony Controller with a view on a js file. That way you can use the twig (or php) template functions (like twig's path() function) to avoid any strange urls.
In your controller:
public function routingAction(Request $request) {
$this->render('angular/routing.twig.js');
}
And in your routing
$routeProvider.
when('/liste-produits', {
templateUrl: {{ path('product_list') }},
controller: 'ProductListCtrl'
}).
otherwise({
redirectTo: '/'
});

Route with name "HelloWorld" not found Zend Framework 2

I have created a new module in ZF2 named 'HelloWorld'. What I am trying to do is, simply printing HelloWorld when I click on the link 'HelloWorld':
I want to generate this link(http://mayukh.my.phpcloud.com/zf2test/HelloWorld/) by using this:
$this->url('HelloWorld', array('action' => 'index'))
But it is showing the error like this:
http://mayukh.my.phpcloud.com/zf2test/
Please suggest how to avoid this error..
This is perhaps related to one of ZF2’s “features.” It seems that if you use ZF2 functions to construct your links, the function will drop out any segment that matches the default value you have named in your router script. See How to write the ZF2 router script to allow parameters on the default action.
Temporarily change or remove the defaults from your router script and see if that doesn’t solve your issue. If it does, you might have to either reconsider the scheme for your router scripts or code your links without ZF2’s url function.

Backbone and Codeigniter - Routing problems

I have the same problem like its asked here Routing problems with Codeigniter and Backbone.js - so, is there an existing example how this is solved? How and where do I have to adjust my code? And how do I dump Codeigniters View part and let Backbone do the complete MV part like it is suggested?
Thanks in advance...
[UPDATE]
So, after looking at Catch-all Controller/Route, in my application->config->routes.php Do I have to add:
$route['(:any)'] = 'catchall_controller'
additionally to $route['default_controller'] = "main" or instead of that?
then in my main Controller I have to do this or what?:
class Main extends CI_Controller {
public function index()
{
$this->uri->segment(n);
$this->load->view('home');
}
}
Finally remove the pushState:true in my Backbone App so it says:
Backbone.history.start({root: App.ROOT });
is this correct?
Your problem comes from the fact you're using pushState. From the documentation (http://backbonejs.org/#History):
Note that using real URLs requires your web server to be able to
correctly render those pages, so back-end changes are required as
well. For example, if you have a route of /documents/100, your web
server must be able to serve that page, if the browser visits that URL
directly.
So your options are basically:
stop using pushState Backbone.history.start({ root: App.ROOT });
make your server return a valid response for every URL your Backbone app can generate (see
Catch-all Controller/Route)
You're running into issues because your client-side applications is generating URLs that your serever doesn't know how to process => 404 error
See also Backbone router: Use hashbangs

How can I bootstrap Symfony into a standalone script?

(I hope 'bootstrap' is the correct term...)
I have a Symfony 1.4 project in which I'm using a PHP script that mostly contains Javascript (I'm including this script with a simple <script src="/js/myStuff.js"></script> tag). I need to use some Symfony classes, helper methods, and variables from within the script (specifically the sfConfig class, url_for() helper method, and the $sf_request variable.) I'm at a loss as to how to achieve this. I tried copying the code from one of the front controllers into the script, but that ended up outputting the contents of my application's layout file.
Thanks in advance!
You can do what you want by using something like this to create a symfony context:
require_once($_SERVER['DOCUMENT_ROOT'].'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
$context = sfContext::createInstance($configuration);
To use url_for, you will also need to either load/include the Url helper, which can be done like:
sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
I think there's a better approach though:
Serve this javascript file as symfony action if you need access to symfony - there's nothing that says you can only serve html through symfony.
Check out the block here entitled Javascript As An Action for an explanation ...
http://www.symfony-project.org/jobeet/1_2/Doctrine/en/18#chapter_18_user_feedback

Categories