Can anyone link me to some Symfony resources, they are hard to find. I am having a little trouble understanding how to use it correctly. Like with CodeIgniter to use the security helper you would load it doing this:
$this->load->helper('security');
And you would use its functions you would do something like this:
$data = $this->input->xss_clean($data);
But with Smyfony to redirect someone to a 404 page, you need to use the sfAction class and the redirect404() api. So could anyone explain or link me a good tutorial?
I would highly recommend you set aside a few hours and read through the Practical Symfony tutorial. It goes through all the basics from project start to end.
Symfony, although a great framework, has a steep learning curve. This tutorial really helps you understand how it works, from the basics to more advanced stuff.
http://www.symfony-project.org/book/1_2/06-Inside-the-Controller-Layer#chapter_06_sub_skipping_to_another_action (scroll down a few paragraphs)
Using
http://www.symfony-project.org/api/1_4/sfAction
You have access to a number of 404 redirection methods (redirect404, forward404, forward404If, forward404Unless) which are detailed on the link above. You have access to all of these methods from within your actions:
public function executeAction(sfWebRequest $request)
{
$this->forward404();
}
I would recommend using forward404 instead of redirect404 as the latter will push the redirection back to the browser and show your user the 404 page's URL instead of the URL they attempted to access.
Configuring
You can configure the module and action that should be executed when a 404 is triggered in your application's config/settings.yml like this:
all:
.actions:
error_404_module: my_module # To be called when a 404 error is raised
error_404_action: my_action # Or when the requested URL doesn't match any route
Update:
For general information on Symfony check out the three books they have available online: http://www.symfony-project.org/doc/1_4/, 'Practical Symfony' being a great place to get started. There is also a complete API reference available: http://www.symfony-project.org/api/1_4/.
Related
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.
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
I am working on a new website being built in SilverStripe. Currently I am having a ton of trouble trying to get the system to let me change the URL alias (or create a second one) for the Security controller's login (and eventually logout) function.
I have tried playing around with the routes.yml file and I tried creating the paths in my own UserController and loading directly from the Security controller with "return Security::login()". But that gives me errors about the use of the static functions.
Unfortunately I don't come from a ton of object oriented experience and this is the first CMS I have used that actually uses a bunch of true object orientation. The current version of SilverStripe we are using is 3.0 (but we will be upgrading to 3.1.1 in a few days).
Does anyone know much about the routing in SilverStripe?
as you stated correctly, SilverStripe has routes, and they can be defined in a yaml config file.
if you take a look at the existing routes.yml in the framework you can see how the default security route is defined:
https://github.com/silverstripe/silverstripe-framework/blob/fd6a1619cb7696d0f7e3ab344bc5ac7d9f6cfe77/_config/routes.yml#L17
if you just want to replace the Secuirty in Secuirty/login, its as easy as just creating your own routes.ymlin mysite/_config/ with the following content:
---
Name: myroutesorsomeotherrandomname
Before: '*'
After:
- '#rootroutes'
- '#coreroutes'
- '#modelascontrollerroutes'
- '#adminroutes'
---
Director:
rules:
'foobar//$Action/$ID/$OtherID': 'Security'
NOTE: make sure you ran a ?flush=1 to ensure the yml file is loaded (they get cached)
also make sure you use spaces in the yml file, if you use tabs you are going to have a bad time
if you however wish to also replace /login and /logout this is no longer a thing for routing.
login and logout are actions (php functions that are listed in Security::$allowed_actions and thus available as URL) on the on Security.
but its still rather easy, just subclass Security and create the actions you want:
<?php
class MySuperSecurity extends Security {
private static $allowed_actions = array(
'showMeTheLoginForm',
'alternative_login_action_with_dashes',
'doTheLogout',
);
function showMeTheLoginForm() {
// can be visited via http://website.com/foobar/showMeTheLoginForm
return $this->login();
}
function alternative_login_action_with_dashes() {
// can be visited via http://website.com/foobar/alternative-login-action-with-dashes
return $this->login();
}
function doTheLogout($redirect = true) {
// can be visited via http://website.com/foobar/doTheLogout
return $this->logout($redirect);
}
}
and make the route point to your new class instead of Security inside the routes.yml:
'foobar//$Action/$ID/$OtherID': 'MySuperSecurity'
NOTE: again, make sure you did a ?flush=1, both the private static $allowed_actions as well as the yml config file are cached by silverstripe.
NOTE: both solutions suggested in this post will create an additional route to login and does not replace the existing one, so the old Security/login will still be available
I don't know nothing about SilverStripe excepting that is a CMS, but i think SilverStripe must provide a way to aliases Url. Also an alternative is create Alias in virtual host definition if you're using apache or in .htaccess file. Refer to apache doc to further details. If you post a skeleton of your .htaccess file or VirtualHost definition i could help you.
I need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
Let's pretend I'm trying to learn CI, and as my test project I am building a group-buying site.
What I'd like is to have a different page for each city, e.g.:
http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/
I'd also like to have different pages such as:
http://www.groupon.com/learn
http://www.groupon.com/contact-us
If I am building this in CI and following the MVC ideology, how would this work? I'm having difficulty seeing how to accomplish the desired URL's with the concept of:
http://www.domain.com/controller/view/segment_a/segment_b/etc...
What I would do is create a custom 404 controller that acts as a catch-all for non-existent routes.
It would take the URI, possibly validate it, and re-route it to the (e.g.) "city" controller.
If the city controller can't find the city (whatever string was specified), then it needs to issue a proper 404. Otherwise, you're good to display your information for that city.
Also, once you create your custom 404 controller, you can send all 404 errors to it by specifying a route named '404_override'.
That's where URI Routing comes in. But in your case you'll probably will have to be carefull defining your routes as the first and only part of your route is a variable part already.
This really has nothing to do with MVC, and much more to do with good URL.
You're looking for URLs that are both (a) clear from the user's point of view and (b) that give hints to your application as to how it's meant to be handled.
What I'd do in this case is redesign your URLs slightly so that rather than:
http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/
You would have URLs that looks like this:
http://www.groupon.com/destinations/las-vegas/
http://www.groupon.com/destinations/orlando/
The bit at the beginning--/destinations/--can be used by your URL routing code to decide what controller should be dealing with it. If your routing code is URL-based, you might have an array like this:
$routes = array(
'/destinations/' => 'on_destination_list',
'/destinations/(.+)' => 'on_destination',
'/(.*)' => 'on_page');
// Basic URI routing code based off of REQUEST_URI
foreach ($pattern => $func) {
if (preg_match("`^$pattern$`", $_SERVER['REQUEST_URI'], $placeholders)) {
array_shift($placeholders);
call_user_func($func, $placeholders);
}
}
Keep in mind that I wrote that routing code off the top of my head and it may not be absolutely correct. It should give you the gist of what you need to do.
Doing things this way has the added benefit that if somebody goes to http://www.groupon.com/destinations/, you'll have the opportunity to show a list of destinations.