I have a custom Silex\RouteCollection which I want to register...
class RouteCollectionProvider extends RouteCollection
{
public function __construct() {
$this->add(
'Index',
new Route('/', array(
'method' => 'get',
'controller' => 'index',
'action' => 'index'
)
));
}
}
...during the bootstrapping:
$app = new Silex\Application();
/** here **/
$app->run();
I could use:
$app = new Silex\Application();
$routes = new RouteCollectionProvider();
foreach ($routes->getIterator() as $route) {
$defaults = $route->getDefaults();
$pattern = $route->getPath();
$callback = 'Controller\\'
. ucfirst($defaults['controller'])
. 'Controller::'
. $defaults['action']
. 'Action';
$app->get($pattern, $callback);
}
$app->run();
I don't like having the initialization of those routes right in there.
Do you know any spot in Silex, where this does fit better?
I cannot use $app->register() because it's getting called too late and the routes won't get active in there.
Maybe there is an event I can use with
$app->on('beforeCompileRoutesOrSomething', function() use ($app) {
// initialize routes
}
Or a hook in the Dispatcher?
My aim is to not have a big collection of $app->get() or $app->post() in there. I also know I can ->mount() a controller but then still I have all my get definitions in my bootstrap and not in a Provider.
This post solves the problem: Scaling Silex pt. 2.
$app = new Application;
$app->extend('routes', function (RouteCollection $routes, Application $app) {
$routes->addCollection(new MyCustomRouteCollection);
return $routes;
});
$app->run();
Related
Hello I am having an issue while rendering the page, i hope someone can help me with it.
I have tried changing the directory structure as well creating a new project with similar code , but nothing works for me .
routes.php
<?php
$app->get('/home' , function() {
return $this->view->render($response,'home.twig');
});
app.php
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new \Slim\App([
'settings'=> [
'displayErrorDetails' => true,
]
]);
$container = $app->getContainer();
// Register component on container
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__.'/../resources/views', [
'cache' => __DIR__.'../cache',
]);
// Instantiate and add Slim specific extension
$view->addExtension(new Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
return $view;
};
require __DIR__ . '/../app/routes.php';
index.php
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
See Slim templates section.
The closure needs extra parameters in routes.php:
// Render Twig template in route
$app->get('/home', function ($request, $response) {
return $this->view->render($response, 'home.twig');
});
http://www.slimframework.com/docs/tutorial/first-app.html shows using "plain old PHP" (Composer package is php-view and class is PhpRenderer) for templates as follows:
<?php
// Create app
$app = new \Slim\App();
// Get container
$container = $app->getContainer();
$container['view'] = new \Slim\Views\PhpRenderer("../templates/");
$app->get('/tickets', function (Request $request, Response $response) {
$this->logger->addInfo("Ticket list");
$mapper = new TicketMapper($this->db);
$tickets = $mapper->getTickets();
$response = $this->view->render($response, "tickets.phtml", ["tickets" => $tickets]);
return $response;
});
// Run app
$app->run();
The above page states using Twig for templates is basically the same, but http://www.slimframework.com/docs/features/templates.html shows a very different approach. Which is the correct way to use templates, and specifically how should I use Twig for templates?
<?php
// Create app
$app = new \Slim\App();
// Get container
$container = $app->getContainer();
// Register component on container
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig('path/to/templates', [
'cache' => 'path/to/cache'
]);
$view->addExtension(new \Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
return $view;
};
// Render Twig template in route
$app->get('/hello/{name}', function ($request, $response, $args) {
return $this->view->render($response, 'profile.html', [
'name' => $args['name']
]);
})->setName('profile');
// Run app
$app->run();
I have the following index.php:
<?php
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\Micro;
try {
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(array(
__DIR__ . '/controllers/',
__DIR__ . '/models/'
))->register();
// Create a DI
$app = new Micro();
$di = new FactoryDefault();
$app->setDI($di);
// Setup the view component
$di->set('view', function () {
$view = new View();
$view->setViewsDir('/views/');
return $view;
});
// Setup a base URI so that all generated URIs include the "legacy" folder
$di->set('url', function () {
$url = new UrlProvider();
$url->setBaseUri('/legacy/api/v1/');
return $url;
});
$di->set('router', function() {
// require __DIR__.'/config/routes.php';
// return $router;
$router = new Phalcon\Mvc\Router(false);
$router->add('/', array(
"controller" => "site",
"action" => "index"
));
$router->notFound(
[
"controller" => "site",
"action" => "ping"
]
);
var_dump($router);
return $router;
});
$app->handle();
} catch (\Exception $e) {
// Do Something I guess, return Server Error message
$app->response->setStatusCode(500, "Server Error");
$app->response->setContent($e->getMessage());
$app->response->send();
}
and the following structure:
--api
----v1
------config
-------- routes.php
------controllers
--------SiteController.php
------models
------views
The problem is that I think my application ignores the router, because I'm getting the following error: if I navigate to "/" - Matched route doesn't have an associated handler. While if I go to some random location like "/12321" it returns Not-Found handler is not callable or is not defined.
Any idea what I'm doing wrong?
Remember that you're setting as base uri /legacy/api/v1/ so all your routes are intended to be appended to that.
$di->set('url', function () {
$url = new UrlProvider();
$url->setBaseUri('/legacy/api/v1/');
return $url;
});
You can't simply access: / but instead, you've to visit /legacy/api/v1/ or if you want to visit /user, you've to visit /legacy/api/v1/user.
Greetings!
I also had some problems with phalcon (1.3.x) routes too. Here's what I did to make them working the way I wanted. Took me a long time to figure that out:
/** #var \Phalcon\Mvc\Router $router */
$router = new Phalcon\Mvc\Router(FALSE); // does not create default routes
$router->removeExtraSlashes(TRUE);
$router->setDefaults(array(
'namespace' => 'Namespace\Controller',
'controller' => 'index',
'action' => 'index'
));
$router->notFound(array(
'controller' => 'index',
'action' => 'show404'
));
Hope it helps.
I am trying to setup Slim Framework with the use of Smarty, but something is very wrong.
I can output the template, but it renders the template with the markers and the the data which should replace the markers. In the .tpl I have a marker {#currency#}, but this is also what is printed when I call test.php/test/1
I have this in my test.php
require 'libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
use Slim\Slim;
require 'libs/Slim/Extras/Views/Smarty.php';
$app = new Slim(array('view', new \Slim\Extras\Views\SmartyView()));
$app->get('/test/:id', function($id) use ($app) {
$adverts = new Adverts();
$app->render('viewad.tpl', array(
'viewad' => $adverts->viewsinglead($id),
'imagelist' => $adverts->getadimages($id),
'firstimage' => $adverts->getadimage($id)
));
});
$app->run();
In libs/Slim/Extras/Views/Smarty.php I have set this:
public static $smartyDirectory = '/var/www/vhosts/xxxxx.dk/web/libs/smarty/libs';
public static $smartyCompileDirectory = '/templates_c';
public static $smartyCacheDirectory = '/cache';
public static $smartyTemplatesDirectory = '/templates';
Had an error in the array!
This fixed it:
$app = new Slim(array('view' => new \Slim\Extras\Views\SmartyView()));
By default the page is set like this in the Application module.config array:
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml'
I want to change the page. I want new ViewModel for it full of variables. It means that simply to change the template is not enough:
'error/404' => __DIR__ . '/../view/error/my_new_404_template.phtml'
But I can't understand how to make it. I can't see the way the request comes to the 'error/404'.
How to create new ViewModel for it?
How to attach variables to it?
How the route comes to 'error/404' to catch it to change?
For example, I have such a method for 'error/404' page:
public function pageNotFoundAction() {
$view = new ViewModel();
$view->setTemplate('error/404'); // set my template
$sm = $this->getServiceLocator()->get('SessionManager');
$cont = new Container('SomeNamespace', $sm);
$view->var1 = $cont->offsetGet('someValue1'); // the "error/404" template
$view->var2 = $cont->offsetGet('someValue2'); // is full of variables
$view->var3 = $cont->offsetGet('someValue3');
$view->var4 = "One more view variable";
// And now I return it somewhere and want it to be called
// in case of "the page is not found"
return $view;
}
How to make such a change? I can't get the system they've created to deal with things like 'error/404'. Please help.
UPD 1
Even more complicated task. How to have several 'error/404' pages?
Would like to ask guys who created the framework whether they know that 'not_found_template' cannot have an array of 'error/404' pages. How it can be? If I set this option like this:
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml',
'my-page/one_more_error404' => __DIR__ . '/../view/my-page/my-page/one_more_error404.phtml',
'other_page/second_404' => __DIR__ . '/../view/other-page/one_more_error404.phtml',
'not_found_template' => array(
'error/404',
'my-page/one_more_error404',
'other-page/second_404',
);
it throws an error. 'not_found_template' force you to have only one 'error/404' template?
There is another way. To catch an EVENT_DISPATCH_ERROR and totally rebuild viewModel. Cavern is that layout – is a root viewModel, and content appended into layout by default is another viewModel (child). These points are not such clear described in official docs.
Here is how it can look like in your Module.php:
public function onBootstrap(MvcEvent $event)
{
$app = $event->getParam( 'application' );
$eventManager = $app->getEventManager();
/** attach Front layout for 404 errors */
$eventManager->attach( MvcEvent::EVENT_DISPATCH_ERROR, function( MvcEvent $event ){
/** here you can retrieve anything from your serviceManager */
$serviceManager = $event->getApplication()->getServiceManager();
$someVar = $serviceManager->get( 'Some\Factory' )->getSomeValue();
/** here you redefine layout used to publish an error */
$layout = $serviceManager->get( 'viewManager' )->getViewModel();
$layout->setTemplate( 'layout/start' );
/** here you redefine template used to the error exactly and pass custom variable into ViewModel */
$viewModel = $event->getViewModel();
$viewModel->setVariables( array( 'someVar' => $someVar ) )
->setTemplate( 'error/404' );
});
}
I used this to manage 404 error (I moved my website from spip to ZF2 based cms) :
In module onBootstrap function :
$eventManager->getSharedManager()->attach('*', MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -100);
Then
public function onDispatchError(MvcEvent $event)
{
$response = $event->getResponse();
if ($response->getStatusCode() == 404) {
$url = $event->getRouter()->assemble(array(), array('name' => 'index'));
$requestUri = $event->getRequest()->getRequestUri();
$response->getHeaders()->addHeaderLine('Location', "$url?url=$requestUri");
$response->setStatusCode(200);
$response->sendHeaders();
$event->stopPropagation(true);
} elseif($response->getStatusCode() == 500){
//DO SOMETHING else?
return;
}
}
In this code we never return a 404 error, we just call the route (in my example index) with the requested url as param
I hope that help you.
I am not sure I follow what you are trying to achieve, can you give a clear example of what you are trying to do?
If you are simply trying to add variables to the view model passed you could even do this in your controller, check out AbstractActionController
/**
* Action called if matched action does not exist
*
* #return array
*/
public function notFoundAction()
{
$response = $this->response;
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('action', 'not-found');
if ($response instanceof HttpResponse) {
return $this->createHttpNotFoundModel($response);
}
return $this->createConsoleNotFoundModel($response);
}
/**
* Create an HTTP view model representing a "not found" page
*
* #param HttpResponse $response
* #return ViewModel
*/
protected function createHttpNotFoundModel(HttpResponse $response)
{
$response->setStatusCode(404);
// Add in extra stuff from your ServiceLocator here...
// $viewModel->setTemplate(..);
return new ViewModel(array(
'content' => 'Page not found',
));
}
The First thing is to create the views :
modules/Yourapplication/view/yourapplication/error/index.phtml
modules/Yourapplication/view/yourapplication/error/404.phtml
The second thing is to register the views in the module config:
In module.config.php of your application
'view_manager' => array(
//[...]
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
//[...]
'error/404' => __DIR__ . '/../view/yourapplication/error/404.phtml',
'error/index' => __DIR__ . '/../view/yourapplication/error/index.phtml',
),
// [...]
),
in module.php you can also change template and layout.
function onBootstrap(EventInterface $e) {
$app = $e->getApplication();
$evt = $app->getEventManager();
$evt->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this,'onDispatchError'), 100);
}
function onDispatchError(MvcEvent $e) {
$vm = $e->getViewModel();
$vm->setTemplate('layout/blank');
}
Or more simply (where you want) :
/* #var \Zend\Mvc\View\Http\RouteNotFoundStrategy $strategy */
$strategy = $this->getServiceLocator()->get('HttpRouteNotFoundStrategy');
$strategy->setNotFoundTemplate('application/other/404');
$view = new ViewModel();
//$view->setTemplate('application/other/404');
return $view;
You should first detach default notfoundstrategy and in your Module.php and in case of a 404, you should return a new viewmodel from the controller. Please see this post: http://www.cagataygurturk.com/zf2-controller-specific-not-found-page/