Symfony 2 routing doesnt work in prod - php

im developed a pretty simple blog in symfony.
There are 2 bundles: blog and admin which are both fine in dev enviroment.. however the main admin route doesnt seem to work in prod as it throws a 404. Every other route (e.g. /admin/categories and so on) works but /admin works only if i write /app_dev.php/admin. Already cleared the cache.
I dont think that i inserted some code to block admin cos i dont know where could i do such things..
Also dont know exactly what code snippets need for debugging sorry but i will update if anybody ask for one.
Admin route:
admin_image_upload:
path: /imageupload
defaults: { _controller: SzoBeszAdminBundle:Admin:imageUpload }
admin_posts:
path: /admin
defaults: { _controller: SzoBeszAdminBundle:Admin:index }
admin_posts_paginated:
path: /admin/posts/{pageNumber}
defaults: { _controller: SzoBeszAdminBundle:Admin:index }
admin_categories:
path: /admin/categories
defaults: { _controller: SzoBeszAdminBundle:Admin:category }
admin_category_submit:
path: /admin/categorysubmit
defaults: { _controller: SzoBeszAdminBundle:Admin:categorySubmit }
admin_post_submit:
path: /admin/postsubmit
defaults: { _controller: SzoBeszAdminBundle:Admin:postSubmit }
requirements:
_method: GET|POST
admin_post_edit:
path: /admin/post/edit/{id}
defaults: { _controller: SzoBeszAdminBundle:Admin:postEdit }
requirements:
id: \d+
admin_post_delete:
path: /admin/post/delete/{id}
defaults: { _controller: SzoBeszAdminBundle:Admin:postDelete }
requirements:
id: \d+
admin_category_edit:
path: /admin/category/edit/{id}
defaults: { _controller: SzoBeszAdminBundle:Admin:categoryEdit }
requirements:
id: \d+
admin_category_delete:
path: /admin/category/delete/{id}
defaults: { _controller: SzoBeszAdminBundle:Admin:categoryDelete }
requirements:
id: \d+
Blog route:
blog_homepage:
path: /
defaults: { _controller: BlogBundle:Main:index }
requirements:
_method: GET
blog_homepaginated:
path: /page/{pageNumber}
defaults: { _controller: BlogBundle:Main:index }
blog_categorypage:
path: /{theCategory}
defaults: { _controller: BlogBundle:Main:showCategory }
requirements:
_method: GET
blog_categorypaginated:
path: /{theCategory}/page/{pageNumber}
defaults: { _controller: BlogBundle:Main:showCategory }
requirements:
_method: GET
blog_tagpage:
path: /tag/{tag}
defaults: { _controller: BlogBundle:Main:tag }
blog_showpost:
path: /{theCategory}/{title}
defaults: { _controller: BlogBundle:Main:showPost }
requirements:
_method: GET
blog_tagpaginated:
path: /tag/{tag}/page/{pageNumber}
defaults: { _controller: BlogBundle:Main:tag }
Security:
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
szobeszadmin: { password: ***, roles: [ 'ROLE_SUPER_ADMIN' ] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin_secured:
pattern: ^/
anonymous: ~
http_basic:
realm: "Secured Area"
access_control:
- { path: ^/admin, roles: ROLE_SUPER_ADMIN }

Make sure you register ALL YOUR CREATED BUNDLES in Kernel are here, it is a prod section:
$bundles = array(...);
Leave registered dev bundles like this:
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
Also if in app.php file you see false (parameter is for testing):
$kernel = new AppKernel('prod', false);
Change false to true:
$kernel = new AppKernel('prod', true);
Hope it helped, have a nice day.

Make sure you have the mod_rewrite module enabled in Apache.
See here for some help.

The problem was that i created an admin folder inside the web folder so apache tried the /admin route with that folder instead of the route i set.

Related

How to prevent browser back button in Symfony 2.8?

Working on the security.yml file to create a reserved area as I can. How to prevent the browser's return button?
This is the content of my security.yml file:
# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
encoders:
AppBundle\Entity\User: bcrypt
Symfony\Component\Security\Core\User\User: bcrypt
providers:
my_provider:
entity:
class: AppBundle:User
property: username
in_memory:
memory:
users:
admin: { password: $2y$13$voW4Dn5zM/uCMVcDM16KKeupoIMg2uf6t34SIhlZ6F7aIxEUKovk. }
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
secured_area:
anonymous: ~
http_basic: ~
pattern: ^/
form_login:
login_path: /login
check_path: /login
username_parameter: _username
password_parameter: _password
always_use_default_target_path: true
default_target_path: /home
failure_path: /login
remember_me: false
logout:
path: /logout
target: /login
invalidate_session: true
access_denied_handler: app.security.access_denied_handler
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# https://symfony.com/doc/current/security/form_login_setup.html
#form_login: ~
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/$, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/home, roles: [ROLE_ADMIN, ROLE_TEACHER] }
- { path: ^/prodotti, roles: ROLE_ADMIN }
This is my controller file:
class SecurityController extends Controller {
public function homeAction(Request $request) {
if($this->get('security.context')->isGranted('ROLE_TEACHER')) {
}else {
return $this->redirect('http://symfony3.loc/login');
}
die();
return $this->render('AppBundle:Default:home.html.twig');
}
public function loginAction() {
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('AppBundle:Default:alogin.html.twig', array('last_username' => $lastUsername, 'error' => $error));
}
public function login_checkAction() {
}
public function logoutAction(Request $request) {
$session = new Session();
$session->clear();
return $this->redirect('http://symfony3.loc/login');
}
This is the route file:
home_page:
path: /home
defaults: { _controller: AppBundle:Security:home }
login:
path: /login
defaults: { _controller: AppBundle:Security:login }
logout:
path: /logout
defaults: { _controller: AppBundle:Security:logout }
login_check:
path: /login_check
You can write JavaScript code in your twig to prevent browser's back button to be clicked
<script type="text/javascript">
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>

symfony2 routing, bad parsing

I have an application in symfony2, and i create routing like this:
spec_add:
path: /add.html
defaults: { _controller: MyBundle:Spec:add }
methods: [GET, POST]
spec_add_to_order:
path: /{indent}/add.html
defaults: { _controller: MyBundle:Spec:add }
methods: [GET, POST]
requirements:
indent: \d+
spec_edit:
path: /{id}/edit.html
defaults: { _controller: MyBundle:Spec:add }
methods: [GET, POST]
requirements:
id: \d+
And I have controller SpecController and method:
public function addAction(Indent $indent = null,Specification $specification = null, Request $request)
when I go to address mydomain.dev/{myindentId}/add.html
i get indent object but I get Specification object too, Why? i don't want specification in this case what am I doing wrong?

Symfony2 routing pattern route not found

i have a very simple understanding question
isnt it possible to have such routing patterns ?
foobar_foobar_videos_all:
pattern: /video
defaults: { _controller: foobarfoobarBundle:Content:showVideos }
foobar_foobar_videos_by_category:
pattern: /video/{category}
defaults: { _controller: foobarfoobarBundle:Content:showVideosByCategory }
foobar_foobar_videos_by_category_and_offset:
pattern: /video/{category}/page/{offset}
defaults: { _controller: foobarfoobarBundle:Content:showVideosByCategory }
this way the first route doesnt work, printing
"No route found for "GET /video"
while the others work perfect.
How can i route to /video ?
The solution was to put it the /video pattern to the end
foobar_foobar_videos_by_category:
pattern: /video/{category}
defaults: { _controller: foobarfoobarBundle:Content:showVideosByCategory }
foobar_foobar_videos_by_category_and_offset:
pattern: /video/{category}/page/{offset}
defaults: { _controller: foobarfoobarBundle:Content:showVideosByCategory }
foobar_foobar_videos_all:
pattern: /video
defaults: { _controller: foobarfoobarBundle:Content:showVideos }

Configuring authentication symfony2

I'm trying to configure authentication on symfony2 with this configuration:
Security.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls:
admin_area:
pattern: ^/admin
provider: in_memory
anonymous: ~
form_login:
login_path: login
check_path: login_check
logout:
path: /logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
/src/MyBundle/Resources/Routing.yml
ies_cierva_encuesta_backend_admin:
pattern: /admin
defaults: { _controller: Bundle:Default:admin }
login:
pattern: /login
defaults: { _controller: Bundle:Login:login }
login_check:
pattern: /login_check
logout:
pattern: /logout
src/Bundle/Controller/LoginController.php
<?php
namespace ...
use ...
class LoginController extends Controller {
public function loginAction(Request $request) {
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
SecurityContext::AUTHENTICATION_ERROR
);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render(
'Bundle:Security:login.html.twig',
array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
)
);
}
}
I'm getting this error:
"Unable to find the controller for path "/login_check". Maybe you forgot to add the matching route in your routing configuration?"
If I'm not wrong, this route doesn't need a Controller...
In http://symfony.com/doc/current/book/security.html, it is mentioned that
"Make sure that your check_path URL (e.g. /login_check) is behind the firewall you're using for your form login".
But the /login_check isn't behind the same firewall which you are using for form login.
firewalls:
admin_area:
pattern: ^/admin
provider: in_memory
anonymous: ~
form_login:
login_path: login
check_path: login_check
logout:
path: /logout
target: /
In the above configuration, pattern path "login_check" doesn't match "^/admin" pattern. Change the pattern accordingly to make it work.

Why is symfony2 functional test client->request() not getting successful response()?

So I've decided to delve into phpunit testing and I've shamefully written out my php code before writing my test. Anyway, I'm just writing a very simple test that tells me if I actually found the correct web page. Unfortunately my one assertion test keeps failing. I know my route "/login" is correct because when I navigate to localhost/index.php/login (where index.php is a link to app_dev.php), the page comes up correctly. Bellow is my routing.php file:
caremonk_mainsite_login:
pattern: /login
defaults: { _controller: CaremonkMainSiteBundle:Security:login }
requirements:
_method: POST|GET
caremonk_mainsite_login_check:
pattern: /login_check
requirements:
_method: POST|GET
caremonk_mainsite_signup:
pattern: /signup
defaults: { _controller: CaremonkMainSiteBundle:CreateUser:signup }
requirements:
_method: POST|GET
caremonk_mainsite_logout:
pattern: /logout
defaults: { _controller: CaremonkMainSiteBundle:Security:logout}
requirements:
_method: POST|GET
caremonk_mainsite_post_blog:
pattern: /post_blog
defaults: { _controller: CaremonkMainSiteBundle:UserEvents:post }
requirements:
_method: POST|GET
caremonk_mainsite_my_profile:
pattern: /my_profile_edit
defaults: { _controller: CaremonkMainSiteBundle:UserEvents:editProfile }
requirements:
_method: POST|GET
caremonk_mainsite_activate:
pattern: /activate/{username}/{token}
defaults: { _controller: CaremonkMainSiteBundle:CreateUser:activateAccount }
requirements:
_methods: GET
caremonk_mainsite_password_reset_request:
pattern: /reset_password/
defaults: { _controller: CaremonkMainSiteBundle:Security:passwordResetRequest }
requirements:
_methods: GET | POST
caremonk_mainsite_reset_password_email:
pattern: /reset_password_email/{username}/{resetPasswordToken}
defaults: { _controller: CaremonkMainSiteBundle:Security:sendNewPassword }
requirements:
_methods: GET
caremonk_mainsite_change_password:
pattern: /change_password
defaults: { _controller: CaremonkMainSiteBundle:Security:changePassword }
requirements:
_methods: GET | POST
caremonk_mainsite_home:
pattern: /
defaults: { _controller: CaremonkMainSiteBundle:Home:index }
requirements:
_methods: GET
Anyway bellow is the test code that keeps failing:
<?php
namespace Caremonk\MainSiteBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SecurityControllerFunctionalTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
// I've done many tests
// I've tried the following request with all failed results
// $crawler = $client->request('GET', 'index.php/login');
// $crawler = $client->request('GET', 'http://localhost/indpex.php/login');
// $crawler = $client->request('GET', 'localhost/index.php/login');
// You get the idea
$crawler = $client->request('GET', '/login');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}
My routing.yml and routing_dev.yml files are shown bellow
#routing_dev.yml
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
#routing.yml
caremonk_main_site:
resource: "#CaremonkMainSiteBundle/Resources/config/routing.yml"
prefix: /
You prefixed your imported routes with a "/", and your routes path start with a "/".
Normally I would prefix my routes with something more meaningful (and does not end with a "/") or remove the "/" from your imported routes.
Running the following command should give you insight to how your routes are registered.
app/console debug:router

Categories