Backbone and Codeigniter - Routing problems - php

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

Related

How do I redirect URLs in a Laravel project?

I have experience using .htaccess files to redirect user requests from one place to another. However, I am new to Laravel and have never attempted to do redirects in Laravel.
What's the best way to do redirects in Laravel? Is it as simple as putting an .htaccess file in the project root and doing things like I normally would, or does Laravel have a special way of doing this?
For my specific use case, I want to redirect requests to images in an img directory to another publicly accessible path.
Thank you.
Laravel has a helper function for redirects, here are some examples:
Route::get('dashboard', function () {
return redirect('home/dashboard');
});
or from inside of an controller action:
return redirect()->action('HomeController#index');
Official Laravel Redirect Documentation
Everything is available at Laravel Documentation but you need to look at it. In your case you can use global redirect helper:
Route::get('route', function () {
return redirect('anotherRoute');
});

CodeIgniter - Select controller based on database

I'm building a simple CMS using Code Igniter version 3.0.0
The site's URLs are all customizable by the user and so do not follow the standard MVC structure of /controller/method/parameter-1/parameter-2/. Instead, all frontend traffic gets directed to PublicController's index method. This method searches the database for the current URL to return the correct page, and also the page type. Each page type corresponds to a controller.
How do I call that controller from the PublicController without doing a redirect?
I can't use the redirect() method because that would change the URL in the browser window and cause an un-need additional page request.
if you look at the url /about/who-we-are/
about is the controller and who-we-are is a function in the controller that loads one or more views.
The same for /locations/stores/
the functions stores in the controller locations.
read the documentation and it will be easy to understand.
http://www.codeigniter.com/user_guide/overview/mvc.html
I am pretty sure that configuring a route is your answer:
// routes.php
$route['(:any)'] = "PublicController/index/$1";
// PublicController.php
public function index()
{
var_dump(func_get_args());
}

Running my Backbone Render function on a selected route?

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.

Incorrect routing via /config/routes.php

I have the following (basic) route set up in a CI-based web app:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
The controller + 'resend' method:
class Sms extends CI_Controller {
public function resend($to, $message) {
// my code
}
}
Logically speaking, anything that doesn't fit the route should be directed to a 404 page instead of the resend() method within the sms controller. This isn't the case, however. The following URL, for example, isn't redirected correctly, it goes to the same controller+method:
http://myapp/sms/resend/uuuu/WhateverMessage
What could be the problem?
After a bit of digging, I've come to understand that CI's default routing does not get deactivated when a default route related to a specific controller/method pair is added. That being said, if a URL does not fit the route $route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2', then the same URL is run through CI's default routing mechanism as a fallback, so it still takes me to the resend method of the sms controller. To prevent this from happening, I needed to add another custom route that follows all others related to the sms resending, that redirects any other url to a different controller+method. If this controller doesn't exist, you get the default 404 page.
So the final /config/routes.php file:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
$route['sms/checkoperator/(:num)'] = 'sms/checkoperator/$1';
$route['sms/(:any)'] = 'somewhereovertherainbow';
I think the rout file is just for rerouting. Your URL don't fits the routing Conditions so it don't gets rerouted! So it goes the normal way wich is the same (in this case!)
Something like this could work!
(! :num) /(:any) '] = error page (or not existing page)
So every request wich doesn't start with a number gets redirected to the error page!
The syntax might be wrong!
This would work great :
$route['sms/resend/[^0-9]/(:any)'] = 'errorpage';
You have to replace the error page by something ;)

CodeIgniter htaccess Redirect

I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.
Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?
Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.
Any suggestions?
CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:
http://localhost/user/info/gregavola
Then in the user controller, add the following stub:
function info($name)
{
echo $name;
}
From here you would probably want to create a view and pass $name into it:
$data['name'] = 'Your title';
$this->load->view('user_info', $data);
You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.
To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:
$route['(:any)'] = "user/info/$1"
Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.
Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html
Good luck!

Categories