phalcon router not working - php

I try to use a router with phalcon. This is how it is included in index.php right after registering the 'events manager':
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
and this is how the routes.php looks like:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add("/", array(
'controller' => 'index',
'action' => 'index'
));
$router->add("/topics", array(
'controller' => 'wurst',
'action' => 'index'
));
$router->handle();
return $router;
The website reacts as if the router was not existent. /topics and topics say this:
TopicsController handler class cannot be loaded
and I also cannot use a die("test"); function inside routes.php . nothing happens.
I also tried to activate it without a separate file, but the result was the same :(
(The sample web-application INVO was used as starting point for my site )

$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); will use default $_SERVER['REQUEST_URI']

If your index/index action is working when you access domain.com/index.php, check that you are using proper uri source, if using nginx or php built-in server you might have some problems with routing and $_GET['_uri'] which phalcon use for handling uris.
can find more about it on phalcon router documentation about uri sources -> http://docs.phalconphp.com/en/latest/reference/routing.html#uri-sources

now it seems to work:
Action 'route404' was not found on handler 'index'
the problem was , that I put the function to set the router in index.php within "set dispatcher function". ..did not see the closing braces.

Related

Unable to unset Phalcon aliases

I am using INVO example app from here:
https://github.com/phalcon/invo
I have copied all the files and set the db and base url.
It works, I can login etc.
however, I wanted to learn how to use redirects e.g.
I would like to use contact-us instead of contact without changing the name of the controller.
So, I created a file routes.php inside of the app/config folder and put this inside:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');
$router->handle();
return $router;
and I have created this in my bootstrap file index.php in the public root directory
$di = new \Phalcon\DI\FactoryDefault();
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
However, it's not working and when I try to access http://localhost/test/contact-us it works, but http://localhost/test/contact-us stopped working and I am redirected to the homepage.
If I (comment the route) do this:
/*$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');*/
$router->handle();
return $router;
Neither http://localhost/test/contact-us nor http://localhost/test/contact-us works ;(.
If I uncomment it back. contact-us works but contact don't.
I guess it's b/c of ->setName('contact') and it's stored in the memory or in some file.
how to get it back to the original state and "unset" that?
You can create two routes and each will work:
$router->add('/contact', array(
'controller' => "contact",
'action' => "index"
));
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
));
Or use groups of routes.
So, if you have ContactController and don't want to access it using /contact url you should add router to the DI container. After that the pages will be available only via router and default /controller/action request format will not work.

Structure of api versioning system in PHP

So I want to add backward in-compatible changes to my API.
I was thinking of doing the following.
All of my api endpoints are accessed as follows:
/v2/account
/v2/order
Having a v2 controller that is passed an API version and calls the appropriate function in version specific controllers that are subclass to v2.
so for version 2013_02_13 it calls v2_2013_02_13::account for account api call
How would you implement an API versioning system to support backward incompatible changes using a PHP mvc framework?
In Kohana you could just use a directory for that. So your controllers would be placed like this.
application/classes/Controllers/V2_2013_02_13/Enpoint1Controller
Than you can set up routes for your different versions.
Route::set('v2', 'v2/<controller>(/<action>)')
->defaults(array(
'directory' => 'V2_2013_02_13',
'controller' => 'welcome',
'action' => 'index',
));
This would be the most easy approach, however if you really want to have some dynamic way to call specific versions of your controllers than I would look at the HMVC of Kohana.
My guess would be, that you need an entry controller for each version you have and do an internal request to the correct controller.
Maybe something like this.
Route:
Route::set('versioned', '<version>/<someAction>')
->defaults(array(
'version' => 'v2',
'someAction' => 'user'
'controller' => 'welcome',
'action' => 'index',
));
Controller:
class Controller_Welcome extends Controller {
public function action_index()
{
// Your Version and the action
$this->request->param('version');
$this->request->param('someAction');
// Do an internal request to the right controller (the v2/user is an example)
$internalRequest = Request::factory('v2/user');
}
}
I hope this helps.

Routing in Kohana 3.3.1

I recently started working with the kohana 3.3.1 framework and ran into some problems.
I want to create different routes for different entry points. Right now, this is the default route, which seems to work fine(I think):
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
When I go to the website, it displays 'hello, world!'.
I have a controller called Street, located in application/classes/Controller/Street.php.
The code to this controller is:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Street extends Controller {
public function action_index()
{
$this->response->body('hello, street!');
}
The problem is, that I want to type /something behind the URI, and it should redirect to the defined controller, and action. But it doesn't seem to work. I get 404 error's when I type ANYTHING behind the default URI.
For routes, i use this
/**
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/
Route::set('test1', 'street/<id>')
->defaults(array(
'controller' => 'street',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Any help would be gladly appreciated.
EDIT
I just tried #Darsstar 's instruction to go to /index.php/street, and it worked!
But now, when I try to go to /index.php/street/derp, I get an error saying
The requested URL derp was not found on this server.
So it's not yet working properly I guess
Since the /index.php/street version works go and read the Clean URLs tutorial if you haven't already. If you have, double check everything!
If you have just those two routes, in that order, /index.php/street/derp should have matched the route 'test1'.
The error message 'The requested URL derp was not found on this server.' says you went to /index.php/derp, not index.php/street/derp. Which would match the default route and be dispatched to Controller_Derp::action_index(), but it doesn't exist so Request_Internal::execute() throws a HTTP_Exception_404.
The default route is more of and example. The is a discussion on Kohana's issue tracker to remove it since a catchall default route is a bad practice. Routes should be specific. I recommend to remove it.
And if you think having a catchall route is a good way to catch all 404 requests, please let the Custom Error Pages tutorial prove you wrong.

Zend_Controller_Router_Route causing an 404 error

It's probably late and I have missed this off by a long shot.
I am trying to create a cleaner url structure; so rather than having
/index/about
/index/news
I have
/about
/news
I came across a post on this site which used the following:
public function _initCustomRoute()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default', $route);
}
It rewrites the url within my navigation. So I have created the relevant action and view (tested without the custom route) but I am getting:
Not Found
The requested URL /path/to/public/index.php was not found on this server.
I assume this is a thing that apache does on a windows file system by not adding the drive letter.
I've not touched the .htaccess file.
Any ideas?
Found out why! You must use virtual hosts rather than an alias as I was using.
Full guide here: http://blog.ryantan.net/2010/03/setting-up-wamp-for-zend-framework-projects/

Turn off default routes in Kohana 3?

I believe I know how to do this, but wanted to verify with my awesome community peeps. =)
Here's an example:
I have a Controller class called 'tami', with an action 'index'.
I know that if I want someone to access that controller/action combo via an URL other than "/tami/" or "/tami/index", then I should add a route, via something like this:
Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
->defaults(array(
'controller' => 'tami',
'action' => 'index',
));
But, users can still access this page via /tami/.
How can I turn off the default routing, so that the only valid routes are the ones I define?
I assume I can just remove the default route found in kohana/application/bootstrap.php. Is that correct? Or would that break something else?
I'd say exactly the same as #simshaun — either remove the default route (leaving other controllers unreachable) or check in the before() function in Controller_Tami for the uri to see if it's what you're after.
If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes.
Something like this would take the extra routing logic out of the controller (which is good as we're keeping it in one place):
Route::set('default', function($uri)
{
if ($uri == 'tami' OR $uri == 'tami/index')
{
// Route not allowed by the default methods
throw new Kohana_404_Exception("Route not permitted");
}
},
'(<controller>(/<action>(/<id>)))'
);
Something I haven't yet used but it looks amazingly powerful.
I think the easiest way would be to remove the default route in your bootstrap file, yes. However, any controllers that you have not manually specified a route for can no longer be accessed.
What I would do is create a class, e.g. Controller_Derouter that Controller_Tami extends. Use the before() method in Controller_Derouter to test if the controller was accessed from the default route, and if so, throw a 404. I think you should be able to do that by comparing $this->request->controller against the first URI segment.
Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. If that's the case, you could just implement the before() method directly in the Tami controller.
Maybe like this?
Route::set('pretty_tami', 'these-are-my-initials/<action>')
->defaults(array(
'controller' => 'tami',
));
So there wouldn't be a default action. And you probably want to update the default route (if you still have one) with a regex to exclude tami.
Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));

Categories