I work on Symfony 2.3 and I installed MobileDetectBundle that works on this symfony version:
(in my composer.json)"suncat/mobile-detect-bundle": "0.10.3"
(in packagist)https://packagist.org/packages/suncat/mobile-detect-bundle#v0.10.3
I configured the bundle accordingly
AppKernel.php
new SunCat\MobileDetectBundle\MobileDetectBundle(),
config_mobile.yml
mobile_detect:
redirect:
full:
is_enabled: true # default false
host: http://page.com # with scheme (http|https), default null, url validate
status_code: 301 # default 302
action: redirect # redirect, no_redirect, redirect_without_path
mobile:
is_enabled: true
host: http://m.page.com
status_code: 301
action: redirect
tablet:
is_enabled: true
host: http://t.page.com
status_code: 301
action: redirect
switch_device_view:
save_referer_path: true
service:
mobile_detector: mobile_detect.mobile_detector.default
device_view_class: SunCat\MobileDetectBundle\Helper\DeviceView
request_listener_class: SunCat\MobileDetectBundle\EventListener\RequestListener
extension_class: SunCat\MobileDetectBundle\Twig\Extension\MobileDetectExtension
in my service.yml
services:
mobile_detector: #mobile_detect.mobile_detector.default
in app_mobile.php
$kernel = new AppKernel('mobile', false);
The bundle works with the mobiledetect classes. Both of them are present in my vendor, so I guess that the installation was done properly
I restarted my server, PHP, cleared the cache but still it doesn't show me my redirection in the URL.
Is it possible in some cases that Symfony doesn't recognize a bundle? Does that happens depending on the version you are?
Try this :
mobile_detect:
redirect:
mobile: ~
tablet: ~
switch_device_view: ~
Related
I am using Kratos in my local environment just fine, works great and does what I want it to do, however the main issue I am facing is when I moved my ory kratos location to a droplet and my frontend to my vercel. On the droplet I have setup kratos as show here with nginx. After setting it up I tested my login flow and got these error's when I requested /self-service/registration/flows?id=${flowId}
Thinking I setup my config incorrectly I set it up as seen here.
version: v0.10.1
dsn: postgres://kratos:MYPASSWORD/kratos?sslmode=disable&max_conns=20&max_idle_conns=4
serve:
public:
base_url: http://127.0.0.1:4433/
host: 127.0.0.1
cors:
allow_credentials: true
allowed_origins:
- https://frontend.example.com
allowed_methods:
- POST
- GET
- PUT
- PATCH
- DELETE
allowed_headers:
- Authorization
- Cookie
- Content-Type
exposed_headers:
- Content-Type
- Set-Cookie
enabled: true
admin:
base_url: http://kratos:4434/
host: 127.0.0.1
selfservice:
default_browser_return_url: https://frontend.example.com
allowed_return_urls:
- https://frontend.example.com
methods:
password:
enabled: true
oidc:
enabled: false
flows:
error:
ui_url: https://frontend.example.comerror
settings:
ui_url: https://frontend.example.comsettings
privileged_session_max_age: 15m
recovery:
enabled: true
ui_url: https://frontend.example.comrecovery
verification:
enabled: true
ui_url: https://frontend.example.comverification
after:
default_browser_return_url: https://frontend.example.com/
logout:
after:
default_browser_return_url: https://frontend.example.com/
login:
ui_url: https://frontend.example.com/login
lifespan: 10m
registration:
lifespan: 10m
ui_url: https://frontend.example.com/registration
after:
password:
hooks:
- hook: session
oidc:
hooks:
- hook: session
log:
leak_sensitive_values: false
# set in SECRETS_COOKIE and SECRETS_DEFAULT env variables
secrets:
default:
- 795135465767325463454 #fake
cookie:
- 223108c7839f6324242342 #fake
cookies:
domain: frontend.example.com
path: /
same_site: Lax
session:
lifespan: 72h
cookie:
domain: frontend.example.com
path: /
same_site: Lax
hashers:
argon2:
parallelism: 1
memory: 128MB
iterations: 2
salt_length: 16
key_length: 16
identity:
default_schema_id: default
schemas:
- id: default
url: file:///root/kratos/config/identity.schema.json
courier:
smtp:
connection_uri: smtps://test:test#mailslurper:1025/?skip_ssl_verify=true
The issue still persisted so I checked my code
const req: any = await fetch(`https://frontend.example.com/self-service/${flow}/flows?id=${flowId}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
credentials: 'include',
}).catch((err) => {
throw err;
}); //error
everything seems fine, I have credentials and my Content-Type was correct, Then doing some research I found this https://github.com/ory/kratos/issues/662 I was wondering, would this effect me in my situation (I have my frontend domain https://frontend.example.com and my kratos droplet https://kratos.example.com are examples), and if so how I could go about fixing it(could I use the ory-proxy to get cookies to my application safely?)? My idea was that I could simply setup ory hydra has the oauth provider for my platform but I am not sure.
Also,
Thanks in advance!
I'm trying to implement async HTTP client with Symfony and amphp/http-client, in symfony docs said thatto enable it for HTTP I need to add http_version: '2.0'option to config and I did it:
framework:
http_client:
http_version: '2.0'
scoped_clients:
site:
base_uri: '%env(URL)%'
headers:
x-access-token: '%env(ACCESS_TOKEN)%'
but it's not working and I'm getting error Unrecognized option "http_version" under "framework.http_client". Available options are "default_options", "enabled", "max_host_connections", "mock_response_factory", "scoped_clients".
Any idea what is wrong?? Thanks in advance!
Looks like the docs might need to be updated a bit. Under a 5.2 app:
bin/console config:dump-reference framework http_client
# HTTP Client configuration
http_client:
enabled: true
# The maximum number of connections to a single host.
max_host_connections: ~
default_options:
# Associative array: header => value(s).
headers:
# Prototype
name: ~
# The maximum number of redirects to follow.
max_redirects: ~
# The default HTTP version, typically 1.1 or 2.0, leave to null for the best version.
http_version: ~
So http_version goes under default_options
# config/packages/framework.yaml
framework:
http_client:
default_options:
http_version: '2.0'
bin/console debug:config framework http_client
Current configuration for "framework.http_client"
=================================================
default_options:
http_version: '2.0'
headers: { }
resolve: { }
retry_failed:
enabled: false
retry_strategy: null
http_codes: { }
max_retries: 3
delay: 1000
multiplier: 2
max_delay: 0
jitter: 0.1
enabled: true
scoped_clients: { }
Is there any way to set _locale parameter of routing configuration as a function call or an expression result? I have multiple hosts running on the same symfony app, and there is an i18n turned on. Everything is working fine, but now i need to have another locales set for a specified host.
Right now my routing config looks like
app:
resource: '#AppBundle/Controller/'
...
requirements:
_locale: '%route_locales%'
...
and i have something like this in parameters:
...
route_locales: en|de|fr
...
That would be perfect if i can use something like
"#=service('AppBundle\\\\...\\\\LocalesConfigurator').getLocales()"
as a _locale: value to get this value based on a function call result. Or maybe there are some other options to get another _locale set for a specified host?
You can do it like #Jakumi suggested (via env params passed from server)
or using only application logic:
In route_locales put all possible locales that can be used in any host.
Validate incoming _locale inside "request listener" depending on request host - if some _locale is not allowed for host then return 404 response or show 404 error page etc.
Seems like i've found at least one solutuin, thanks to Jakumi for the idea. I've created a parameter in my config file, which receives an environment varaible. Then i use this parameter in my routing file. The main idea is that i can control this environment variable value using bootstrap file. So it looks like
bootstrap.php (in the example it's just a static value, but in a real life this value will depend on host).
bootstrap.php
...
$_SERVER['SYMFONY__ROUTE__LOCALES'] = 'en|es';
...
config.yml
parameters:
route_locales: '%route.locales%'
routing.yml
app:
resource: '#AppBundle/Controller/'
...
requirements:
_locale: '%route_locales%'
...
I don't realy like this solution, and will be thankful for any better solutions, but at least it does what i want.
I was in the same issue some months ago and I was able to found a solution that worked correctly, but there may also be some better ones.
As I needed different route names to be loaded depending on the locale (i.e. /contact in English and /contacto in Spanish) what I did was creating the methods in the controller and creating an individual link to them in routes.yaml
contact-en:
path: /contact/
controller: App\Controller\ContactController::index
host: domain1.tld
contact-es:
path: /contacto/
controller: App\Controller\ContactController::index
host: domain2.tld
Then I created an EventListener called LocaleSwitcher:
class LocaleSwitcher
{
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$host = $request->getHost();
switch($host) {
case 'domain1.tld': {
$request->setLocale('en');
break;
}
case 'domain2.tld': {
$request->setLocale('es');
break;
}
}
}
}
And then adding it in services.yaml for event kernel.request and priority 20:
App\EventListener\LocaleSwitcher:
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 20 }
I want to cache results of some HTTP requests to my Symfony 3.4 application using Symfony HttpCache.
Following their docs I now have those lines in app_dev.php
$kernel = new AppKernel('dev', true);
$kernel = new AppCache($kernel);
This is my controller code
$overview = $this->channelRepository->getOverview();
$response = new JsonResponse($overview);
$response->setSharedMaxAge(3600);
return $response;
And this is my config.yml
framework:
cache:
app: cache.adapter.redis
default_redis_provider: snc_redis.default
#...
snc_redis:
clients:
default:
type: phpredis
alias: default
dsn: redis://localhost
logging: false
However, I don't see new entries appear in my redis server. Instead, those entries appear in filesystem cache (var/cache/dev/http/cache). How do I fix my application to use Redis HTTP cache?
I dug a bit deeper and found out that AppCache that I use extends Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache which only accepts filesystem store. Does that mean that I should somehow use Symfony\Component\HttpKernel\HttpCache\HttpCache, which accepts any Store in its constructor?
I set my route files in my Symfony2.5 project. But i have this problem when I launched my application with Wamp server (localhost on Windows):
No route found for "GET /" (from "http://localhost/girSymfony/web/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
This is the url used when the application is lauched on WampServer: http://localhost/girSymfony/web/
When I tried with this URL http://localhost/girSymfony/web/I have the 404 error as mentioned above.
But if I try with this url: http://localhost/girSymfony/web/gir/homepage it works; I would like the application arrive and run directly on this url. How can i do that?
This is my Gir/WelcomeBundle/Ressource/config/routing.yml file code:
girWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
And this my app/config/routing.yml file code:
GirWelcomeBundle:
resource: "#GirWelcomeBundle/Resources/config/routing.yml"
prefix: /gir
And here's my controller code for Gir/WelcomeBundle/Controller/HomePageController.php:
<?php
namespace Gir\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomePageController extends Controller
{
public function indexAction()
{
return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
}
}
Like you can see, I set the routes files, but I just forgot something because I have the 404 error.
I think my application don't use the right direction and the right URL , how can I fix it ?
New edit: Note that I 've set WAMP and Symfony in order when I run my application I arrive directly on the HomePage index
You are on development environemt , you should have to use url like this
Example:
registration:
pattern: /registration
defaults: { _controller: UserBundle:Registration:registration }
http://localhost/girSymfony/web/app_dev.php/registration