i made in frontend nice url with symfony routing.yml. In frontend i can use for example:
url_for('#news', $news);
this generate for me:
http://mysite.com/frontend_dev.php/news/nice-title/1
but if i use this in backend i have:
http://mysite.com/backend_dev.php/news/nice-title/1
is possible generate these link in backend for frontend?
In apps/frontend/config/frontendConfiguration.class.php use something like this:
class frontendConfiguration extends sfApplicationConfiguration
{
protected $backendRouting = null;
public function generateBackendUrl($name, $parameters = array(), $absolute = false)
{
return sfConfig::get('app_site_url').$this->getBackendRouting()->generate($name, $parameters, $absolute);
}
public function getBackendRouting()
{
if (!$this->backendRouting )
{
$this->backendRouting = new sfPatternRouting(new sfEventDispatcher());
$config = new sfRoutingConfigHandler();
$routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/backend/config/routing.yml'));
$this->backendRouting->setRoutes($routes);
}
return $this->backendRouting;
}
}
use it like this:
$sf_context->getConfiguration()->generateBackendUrl('my_custom_route', array('id' => 1), true)
More information here: http://symfony.com/blog/cross-application-links
Related
I'm trying to create a View Helper to "inject" a database value to my layout.phtml. It's result with a simple string but when I call a table gateway it's not result and not load the other html.
//Conversa/src/View/Helper/Conversas.php
namespace Conversa\View\Helper;
use Conversa\Model\ConversaTable;
use Zend\View\Helper\AbstractHelper;
class Conversas extends AbstractHelper
{
protected $sm;
protected $mensagemTable;
protected $conversaTable;
public function __construct($sm)
{
$this->sm = $sm;
}
public function __invoke()
{
$id = $_SESSION['id_utilizador'];
//$conversas = $this->getConversaTable()->conversasUtilizadorAll($id);
//$conversaTable = new ConversaTable();
$c = $this->getConversaTable()->fetchAll(); // <-When I call this, it doesn't work anymore
$output = sprintf("I have seen 'The Jerk' %d time(s).", $this->conversaTable);
return htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
}
public function getConversaTable()
{
if (!$this->conversaTable) {
$sm = $this->getServiceLocator();
$this->conversaTable = $sm->get('Conversa\Model\ConversaTable');
}
return $this->conversaTable;
}
public function getMensagemTable()
{
if (!$this->mensagemTable) {
$sm = $this->getServiceLocator();
$this->mensagemTable = $sm->get('Mensagem\Model\MensagemTable');
}
return $this->mensagemTable;
}
}
Module.php
public function getViewHelperConfig()
{
return array(
'factories' => array(
'conversas' => function ($sm) {
$helper = new View\Helper\Conversas;
return $helper;
}
)
);
}
Not much to go on here since you didn't include any info about what happens (error message?), however, your view helper factory doesn't look right. Your view helper constructor method has a required argument for the Service Manager, so you need to pass that:
public function getViewHelperConfig()
{
return array(
'factories' => array(
'conversas' => function ($sm) {
$helper = new View\Helper\Conversas($sm);
return $helper;
}
)
);
}
Also, since your view helper requires conversaTable, it might be better to pass that to the view helper instead of the service manager (as the service locator functionality you're relying on was removed in ZF3).
By providing a URL I would like to know if there's any way to determine if the URL exists in my Laravel application (in comparison to "How can I check if a URL exists via Laravel?" which wants to check an external URL)?
I tried this but it always tells me the URL doesn't match:
$routes = \Route::getRoutes();
$request = \Request::create('/exists');
try {
$routes->match($request);
// route exists
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
// route doesn't exist
}
simply use if Route::has('example')
Route::has('route_name') check if the route exists (on routes files).
Example: test some app routes
<?php
namespace Tests\Feature;
use App\Models\Users\User;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class RouteTest extends TestCase
{
private $routes = [
'home',
'users.index',
'employees.index',
'logs.index',
];
public function setUp(): void
{
parent::setUp();
// Put an admin on session
$adminUser = User::getFirstAdmin();
$this->actingAs($adminUser);
}
public function testRoutes()
{
foreach ($this->routes as $route) {
$this->assertTrue(Route::has($route));
$response = $this->get(route($route));
$response->assertStatus(200);
}
}
}
Edition
The fast way, check with tinker typing on the terminal:
php artisan tinker
then:
Route::has('your_route')
or
route('your_route')
In one of my Laravel application, I achieved it by doing the following
private function getRouteSlugs()
{
$slugs = [];
$routes = Route::getRoutes();
foreach ($routes as $route)
{
$parts = explode('/', $route->uri());
foreach ($parts as $part)
{
$slug = trim($part, '{}?');
$slugs[] = $slug;
}
}
return array_unique($slugs);
}
This function would help to get all the slugs that are registered within Laravel and then with a simple in_array you can check if that slug has been reserved.
EDIT
Based on your comment, you can extend the following function
private function getRouteSlugs()
{
$slugs = [];
$routes = Route::getRoutes();
foreach ($routes as $route)
{
$slugs[] = $route->uri();
}
return array_unique($slugs);
}
That will get you an array of items as such:
0 => "dashboard/news"
1 => "dashboard/post/news"
2 => "dashboard/post/news/{id}"
3 => "dashboard/post/news"
It should be easy enough from here to compare.
I'm relatively new to Slim Framework 3. One thing I'm trying to understand is how to use the router, $this->router, in a "global" template.
What I mean by this is a template such as a navigation menu - something that appears on every page.
For templates I'm using the "php-view" library as per the example tutorial which I installed with:
composer require slim/php-view
In my templates directory I have a file called nav.php where I want to output my links.
I understand how to call the router like so
Sign Up
But... the example tutorial only shows how you would pass that link from 1 individual place, e.g. $app->get('/sign-up' ... })->setName("sign-up");
How can you use the router globally in any template, without passing it into every individual URL route as a parameter?
I'm more familiar with frameworks like CakePHP where there is an "AppController" which allows you to set things globally, i.e. available in every request. I don't know if this is how it's done in Slim but this is the effect I'm after.
Well, you can pass it as template variable.
When you instantiate or register PhpRenderer in a container, you have multiple options to define a "global" variable, i.e. a variable that is accessible in all of your templates:
// via the constructor
$templateVariables = [
"router" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);
// or setter
$phpView->setAttributes($templateVariables);
// or individually
$phpView->addAttribute($key, $value);
Assuming you're registering PhpRenderer via Pimple:
<?php
// Create application instance
$app = new \Slim\App();
// Get container
$container = $app->getContainer();
// Register PhpRenderer in the container
$container['view'] = function ($container) {
// Declaring "global" variables
$templateVariables = [
'router' => $container->get('router')
];
// And passing the array as second argument to the contructor
return new \Slim\Views\PhpRenderer('path/to/templates/with/trailing/slash/', $templateVariables);
};
<?php namespace App\Helpers;
/********************/
//LinksHelper.php
/********************/
use Interop\Container\ContainerInterface;
class LinksHelper
{
protected $ci;
public function __construct(ContainerInterface $container){
$this->ci = $container;
}
public function __get($property){
if ($this->ci->has($property)) {
return $this->ci->get($property);
}
}
public function pathFor($name, $data = [], $queryParams = [], $appName = 'default')
{
return $this->router->pathFor($name, $data, $queryParams);
}
public function baseUrl()
{
if (is_string($this->uri)) {
return $this->uri;
}
if (method_exists($this->uri, 'getBaseUrl')) {
return $this->uri->getBaseUrl();
}
}
public function isCurrentPath($name, $data = [])
{
return $this->router->pathFor($name, $data) === $this->uri->getPath();
}
public function setBaseUrl($baseUrl)
{
$this->uri = $baseUrl;
}
}
?>
<?php
/********************/
//dependencies.php
/********************/
$container['link'] = function ($c) {
return new \App\Helpers\LinksHelper($c);
};
// view renderer
$container['view'] = function ($c) {
$settings = $c->get('settings');
$view = new App\Views\MyPhpRenderer($settings['renderer']['template_path']);
$view->setLayout('default.php');
//$view->addAttribute('title_for_layout', $settings['title_app'] .' :: ');
$view->setAttributes([
'title_for_layout'=>$settings['title_app'] .' :: ',
'link' => $c->get('link')
]);
return $view;
};
?>
<?php
/********************/
//routes.php
/********************/
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$app->get('/', function (Request $request, Response $response, array $args) {
return $this->view->render($response, 'your_view.php');
})->setName('home');
?>
<?php
/********************/
//your_view.php
/********************/
?>
Home
You should create a new class, e.g. MainMenu, and there you should create an array with all paths for menu. Object of MainMenu should return an array with labels and paths and then you can pass that array to your view:
$menu = (new MainMenu())->buildMenu();
$response = $this->view->render($response, "index.phtml", [
'menu' => $menu
]);
Then in your *.phtml file you have access to the $menu variable. But what if you do not want repeat that code in each route?
Use middlewares. You can pass a variable from middleware using
$request = $request->withAttribute('foo', 'bar');
and retrieve
$foo = $request->getAttribute('foo');
I know that I can generate URL passing the route name
<?php echo $this->url('route-name') #in view file ?>
But can I get information in opposite direction?
From current URL/URI, I need to get route name.
Real case is: I have layout.phtml where is the top menu (html).
Current link in the menu need to be marked with css class. So, example what I need is:
<?php // in layout.phtml file
$index_css = $this->getRouteName() == 'home-page' ? 'active' : 'none';
$about_css = $this->getRouteName() == 'about' ? 'active' : 'none';
$contact_css = $this->getRouteName() == 'contact' ? 'active' : 'none';
?>
I am using fast route, but I am interesting in any solution. Solution doesn't have to be in View file.
From my research, there is such information in RouteResult instance in the public method getMatchedRouteName(). The problem is how to reach to this instance from the View.
We know that we can get RouteResult, but from the Request object which is in a Middleware's __invoke() method.
public function __invoke($request, $response, $next){
# instance of RouteResult
$routeResult = $request->getAttribute('Zend\Expressive\Router\RouteResult');
$routeName = $routeResult->getMatchedRouteName();
// ...
}
As #timdev recommended we will find inspiration in existing helper UrlHelper and make almost the same implementation in custom View Helper.
In short we will create 2 classes.
CurrentUrlHelper with method setRouteResult() and
CurrentUrlMiddleware with __invoke($req, $res, $next)
We will inject the CurrentUrlHelper in CurrentUrlMiddleware and
in __invoke() method call the CurrentUrlHelper::setRouteResult() with appropriate RouteResult instance.
Later we can use our CurrentUrlHelper with RouteResult instance in it. Both classes should have an Factory too.
class CurrentUrlMiddlewareFactory {
public function __invoke(ContainerInterface $container) {
return new CurrentUrlMiddleware(
$container->get(CurrentUrlHelper::class)
);
}
}
class CurrentUrlMiddleware {
private $currentUrlHelper;
public function __construct(CurrentUrlHelper $currentUrlHelper) {
$this->currentUrlHelper = $currentUrlHelper;
}
public function __invoke($request, $response, $next = null) {
$result = $request->getAttribute('Zend\Expressive\Router\RouteResult');
$this->currentUrlHelper->setRouteResult($result);
return $next($request, $response); # continue with execution
}
}
And our new helper:
class CurrentUrlHelper {
private $routeResult;
public function __invoke($name) {
return $this->routeResult->getMatchedRouteName() === $name;
}
public function setRouteResult(RouteResult $result) {
$this->routeResult = $result;
}
}
class CurrentUrlHelperFactory{
public function __invoke(ContainerInterface $container){
# pull out CurrentUrlHelper from container!
return $container->get(CurrentUrlHelper::class);
}
}
Now we only need to register our new View Helper and Middleware in the configs:
dependencies.global.php
'dependencies' => [
'invokables' => [
# dont have any constructor!
CurrentUrlHelper::class => CurrentUrlHelper::class,
],
]
middleware-pipeline.global.php
'factories' => [
CurrentUrlMiddleware::class => CurrentUrlMiddlewareFactory::class,
],
'middleware' => [
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
Zend\Expressive\Helper\UrlHelperMiddleware::class,
CurrentUrlMiddleware::class, # Our new Middleware
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
],
And finaly we can register our View Helper in templates.global.php
'view_helpers' => [
'factories' => [
# use factory to grab an instance of CurrentUrlHelper
'currentRoute' => CurrentUrlHelperFactory::class
]
],
it's important to register our middleware after ROUTING_MIDDLEWARE and before DISPATCH_MIDDLEWARE!
Also, we have CurrentUrlHelperFactory only to assign it to the key 'currentRoute'.
Now you can use helper in any template file :)
<?php // in layout.phtml file
$index_css = $this->currentRoute('home-page') ? 'active' : 'none';
$about_css = $this->currentRoute('about') ? 'active' : 'none';
$contact_css = $this->currentRoute('contact') ? 'active' : 'none';
?>
As you note in your self-answer, UrlHelper is a useful thing to notice. However, creating a new helper that depends on UrlHelper (and reflection) isn't ideal.
You're better off writing your own helper, inspired UrlHelper but not dependent on it.
You can look at the code for UrlHelper, UrlHelperFactory and UrlHelperMiddleware to inform your own implementation.
You could wrap the template renderer in another class and pass the Request to there, subtract what you need and inject it into the real template renderer.
Action middleware:
class Dashboard implements MiddlewareInterface
{
private $responseRenderer;
public function __construct(ResponseRenderer $responseRenderer)
{
$this->responseRenderer = $responseRenderer;
}
public function __invoke(Request $request, Response $response, callable $out = null) : Response
{
return $this->responseRenderer->render($request, $response, 'common::dashboard');
}
}
The new wrapper class:
<?php
declare(strict_types = 1);
namespace Infrastructure\View;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Stream;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Template\TemplateRendererInterface;
class ResponseRenderer
{
private $templateRenderer;
public function __construct(TemplateRendererInterface $templateRenderer)
{
$this->templateRenderer = $templateRenderer;
}
public function render(Request $request, Response $response, string $templateName, array $data = []) : Response
{
$routeResult = $request->getAttribute(RouteResult::class);
$data['routeName'] = $routeResult->getMatchedRouteName();
$body = new Stream('php://temp', 'wb+');
$body->write($this->templateRenderer->render($templateName, $data));
$body->rewind();
return $response->withBody($body);
}
}
Code is borrowed from GitHub.
I am trying with no luck to find a "referrer" object for use in my
controller. I expected there would be an object similar to the request
object with parameters specifying the _controller, _route and
arguments.
What I am trying to do is a language switcher action that redirects
the user to the same page in the new language. Something along the
lines of:
public function switchLangAction($_locale)
{
$args = array();
$newLang = ($_locale == 'en') ? 'fr' : 'en';
// this is how I would have hoped to get a reference to the referrer request.
$referrer = $this->get('referrer');
$referrerRoute = $referrer->parameters->get('_route');
$args = $referrer->parameters->get('args'); // not sure how to get the route args out of the params either!
$args['_locale'] = $newLang;
$response = new RedirectResponse( $this->generateUrl(
$referrerRoute,
$args
));
return $response;
}
It's also possible that there is another way to do this - I know in
rails there is the "redirect_to :back" method for example.
Any help would be greatly appreciated.
Why not changing the locale in the user's session?
First, define your locales in the router
my_login_route:
pattern: /lang/{_locale}
defaults: { _controller: AcmeDemoBundle:Locale:changeLang }
requirements:
_locale: ^en|fr$
Then, set the session
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LocaleController extends Controller
{
public function switchLangAction($_locale, Request $request)
{
$session = $request->getSession();
$session->setLocale($_locale);
// ... some other possible actions
return $this->redirect($session->get('referrer'));
}
}
In all other controllers you should set the session variable yourself by calling
$session->set('referrer', $request->getRequestUri());
You could also probably make an event listener to set the session variable for every page automatically.
It's my controller
class LocaleController extends Controller {
public function indexAction()
{
if(null === $this->getRequest()->getLocale()){
$locale = $this->getRequest()->getPreferredLanguage($this->getLocales());
$this->getRequest()->setLocale($locale);
}
else{
$locale = $this->getRequest()->getLocale();
}
return $this->redirect($this->generateUrl('corebundle_main_index', array('_locale' => $locale)));
}
public function changeLocaleAction($_locale)
{
$request = $this->getRequest();
$referer = $request->headers->get('referer');
$locales = implode('|',$this->getLocales());
$url = preg_replace('/\/('.$locales.')\//', '/'.$_locale.'/', $referer, 1);
return $this->redirect($url);
}
private function getLocales()
{
return array('ru', 'uk', 'en');
}
/**
* #Template()
*/
public function changeLocaleTemplateAction()
{
return array();
}
}