请有人帮我这个。每次我输入此网址http://localhost/web/app_dev.php/activity1/时,都不会转到此页面,而是加载此页面http://localhost/web/app_dev.php/users/login。我没有看到它为什么这样做的原因。甚至在重写实体和控制器之后。
实体
<?php
namespace com\twcl\agripayrollBundle\Entity;
/**
* Activity1
*/
class Activity1
{
/**
* @Assert\NotBlank(message="The activity type must be entered.")
* @var string
*
*/
private $activitytype;
/**
*
* @var string
*
*
*/
private $activitydesc;
/**
* @Assert\NotBlank(message="The activity date must be entered.")
* @var \DateTime
*
*
*
*/
protected $activitydate;
/**
* @Assert\NotBlank(message="The activity quantity worked must be entered.")
* @var number
*
*/
private $unitwork;
/**
* @var \DateTime
*
*/
private $dateentered;
/**
* @Assert\NotBlank(message="The employee must be entered.")
* @var integer
*
*/
private $empid;
/**
* @var integer
*/
private $transid;
/**
* @Assert\NotBlank(message="The payroll period must be entered.")
* @var \com\twcl\agripayrollBundle\Entity\Payrollperiod
*/
private $payrollperiodid;
private $userid;
/**
* @Assert\NotBlank(message="The section must be entered.")
* @var \com\twcl\agripayrollBundle\Entity\Section
*/
private $sectionid;
//@ORM\JoinColumn(name="description", referencedColumnName="id")
/**
* @Assert\NotBlank(message="The rate code must be entered.")
* @var \com\twcl\agripayrollBundle\Entity\Rate
*
*/
private $ratecode;
private $versionnumber;
private $verifystate;
public function getVerifystate() {
return $this->verifystate;
}
public function setVerifystate($state) {
$this->verifystate = $state;
}
public function setVersionnumber($versionnumber) {
$this->versionnumber = $versionnumber;
}
public function getVersionnumber() {
return $this->versionnumber;
}
/**
* Set activitytype
*
* @param string $activitytype
* @return Activity
*/
public function setActivitytype($activitytype) {
$this->activitytype = $activitytype;
}
/**
* Get activitytype
*
* @return string
*/
public function getActivitytype() {
return $this->activitytype;
}
/**
* Set activitydesc
*
* @param string $activitydesc
* @return Activity
*/
public function setActivitydesc($activitydesc) {
$this->activitydesc = $activitydesc;
}
/**
* Get activitydesc
*
* @return string
*/
public function getActivitydesc() {
return $this->activitydesc;
}
/**
* Set activitydate
*
* @param \DateTime $activitydate
* @return Activity
*/
public function setActivitydate($activitydate) {
$this->activitydate = $activitydate;
}
/**
* Get activitydate
*
* @return \DateTime
*/
public function getActivitydate() {
return $this->activitydate;
}
/**
* Set unitwork
*
* @param integer $unitwork
* @return Activity
*/
public function setUnitwork($unitwork) {
$this->unitwork = $unitwork;
}
/**
* Get unitwork
*
* @return integer
*/
public function getUnitwork() {
return $this->unitwork;
}
/**
* Set dateentered
*
* @param \DateTime $dateentered
* @return Activity
*/
public function setDateentered($dateentered) {
$this->dateentered = $dateentered;
}
/**
* Get dateentered
*
* @return \DateTime
*/
public function getDateentered() {
return $this->dateentered;
}
/**
* Set empid
* @param integer $empid
* @return Activity
*
*
*/
public function getEmpid() {
return $this->empid;
}
/**
* Get empid
*
* @return integer
*/
public function setEmpid($empid) {
$this->empid = $empid;
}
/**
* Get transid
*
* @return integer
*/
public function getTransid() {
return $this->transid;
}
/**
* Set payrollperiodid
*
* @param \com\twcl\agripayrollBundle\Entity\Payrollperiod $payrollperiodid
* @return Activity
*/
public function setPayrollperiodid(\com\twcl\agripayrollBundle\Entity\Payrollperiod $payrollperiodid = null) {
$this->payrollperiodid = $payrollperiodid;
return $this;
}
/**
* Get payrollperiodid
*
* @return \com\twcl\agripayrollBundle\Entity\Payrollperiod
*/
public function getPayrollperiodid() {
return $this->payrollperiodid;
}
/**
* Set userid
*
* @param \com\twcl\agripayrollBundle\Entity\Users $userid
* @return Activity
*/
public function setUserid(\com\twcl\agripayrollBundle\Entity\Users $userid) {
$this->userid = $userid;
}
/**
* Get userid
*
* @return \com\twcl\agripayrollBundle\Entity\Users
*/
public function getUserid() {
return $this->userid;
}
/**
* Set sectionid
*
* @param \com\twcl\agripayrollBundle\Entity\Section $sectionid
* @return Activity
*/
public function setSectionid(\com\twcl\agripayrollBundle\Entity\Section $sectionid = null) {
$this->sectionid = $sectionid;
}
/**
* Get sectionid
*
* @return \com\twcl\agripayrollBundle\Entity\Section
*/
public function getSectionid() {
return $this->sectionid;
}
//protected $sectionIdOnly;
public function getSectionIdOnly() {
$id = null;
$section = $this->getSectionid();
if ($section!=null){
$id=$this->getSectionid()->getId();
}
return $id;
}
public function setSectionIdOnly ($empty) {
}
public function getFarmNameOnly() {
return $this->farmNameOnly;
}
protected $farmNameOnly;
public function setFarmNameOnly($farmName) {
return $this->farmNameOnly = $farmName;
}
/**
* Set ratecode
*
* @param \com\twcl\agripayrollBundle\Entity\Rate $ratecode
* @return Activity
*/
public function setRatecode(\com\twcl\agripayrollBundle\Entity\Rate $ratecode = null) {
$this->ratecode = $ratecode;
}
/**
* Get ratecode
*
* @return \com\twcl\agripayrollBundle\Entity\Rate
*/
public function getRatecode() {
return $this->ratecode;
}
/**
* Return an Activity as a String
* @return string
*/
public function __toString() {
return strval($this->getTransid());
}
/**
* @Assert\Callback
* validation that ensures: activities can only be entered for days within the payroll period.
*/
public function constraint_within_payroll_period(\Symfony\Component\Validator\Context\ExecutionContextInterface $context) {
/* startDate<=activitydate<=endDate */
$startdate_diff = $this->activitydate->diff($this->payrollperiodid->getstartDate(), false);
$endate_diff = $this->activitydate->diff($this->payrollperiodid->getendDate(), false);
// var_dump($endate_diff);
// echo '<br/>';
// var_dump($endate_diff->invert);
// echo' | ';
// var_dump($endate_diff->days);
// die();
if (
($startdate_diff->invert == 0 && $startdate_diff->days > 0) ||
($endate_diff->invert == 1 && $endate_diff->days > 0 )
) {
$context->buildViolation('Activities can only be entered for dates within the payroll period.')
->atPath('activitydate')
->addViolation();
}
}
/**
* @Assert\Callback
* validation that ensures: The duration of an activity cannot go beyond 24hrs.
*/
public function constraint_capped_duration(\Symfony\Component\Validator\Context\ExecutionContextInterface $context) {
if (
strcmp(trim($this->activitytype), 'Hourly') == 0 &&
$this->unitwork > 24
) {
$context->buildViolation('The duration of an hourly activity cannot go beyond 24hrs.')
->atPath('unitwork')
->addViolation();
}
}
}
控制器
<?php
namespace com\twcl\agripayrollBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use com\twcl\agripayrollBundle\Entity\Activity1;
use com\twcl\agripayrollBundle\Form\Activity1Type;
/**
* Activity1 controller.
*
* @Route("/activity1")
*/
class Activity1Controller extends Controller
{
/**
* Lists all Activity1 entities.
*
* @Route("/", name="activity1")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('comtwclagripayrollBundle:Activity1')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new Activity1 entity.
*
* @Route("/", name="activity1_create")
* @Method("POST")
* @Template("comtwclagripayrollBundle:Activity1:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Activity1();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('activity1_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Activity1 entity.
*
* @param Activity1 $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Activity1 $entity)
{
$form = $this->createForm(new Activity1Type(), $entity, array(
'action' => $this->generateUrl('activity1_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Activity1 entity.
*
* @Route("/new", name="activity1_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new Activity1();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a Activity1 entity.
*
* @Route("/{id}", name="activity1_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('comtwclagripayrollBundle:Activity1')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Activity1 entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Activity1 entity.
*
* @Route("/{id}/edit", name="activity1_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('comtwclagripayrollBundle:Activity1')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Activity1 entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a Activity1 entity.
*
* @param Activity1 $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Activity1 $entity)
{
$form = $this->createForm(new Activity1Type(), $entity, array(
'action' => $this->generateUrl('activity1_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Activity1 entity.
*
* @Route("/{id}", name="activity1_update")
* @Method("PUT")
* @Template("comtwclagripayrollBundle:Activity1:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('comtwclagripayrollBundle:Activity1')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Activity1 entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('activity1_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Activity1 entity.
*
* @Route("/{id}", name="activity1_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('comtwclagripayrollBundle:Activity1')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Activity1 entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('activity1'));
}
/**
* Creates a form to delete a Activity1 entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('activity1_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}
Security.yml
# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
com\twcl\agripayrollBundle\Entity\Users:
algorithm: sha512
encode_as_base64: true
# iterations: 5000
# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
ROLE_VIEW_DATA_OWNER_2: ROLE_USER
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
administrators:
# entity: {class: comtwclagripayrollBundle:Users, property: username}
entity: {class: comtwclagripayrollBundle:Users}
# in_memory:
# memory:
# users:
# user: { password: $2a$12$s3OoYvNBLWjQuW.k76iRoOkdhEfHkU4ARYdkg9cMLKsZRcUPVV3o2, roles: [ 'ROLE_USER' ] }
# admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
default:
anonymous: ~
http_basic: ~
form_login:
login_path: /users/login
check_path: /users/login_check
csrf_provider: form.csrf_provider
logout:
path: /users/logout
target: activity_main
invalidate_session: false
delete_cookies:
a: {path: null, domain: null}
b: {path: null, domain: null}
anonymous: ~
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
# the login page has to be accessible for everybody
demo_login:
pattern: ^/demo/secured/login$
security: false
# secures part of the application
demo_secured_area:
pattern: ^/demo/secured/
# it's important to notice that in this case _demo_security_check and _demo_login
# are route names and that they are specified in the AcmeDemoBundle
form_login:
check_path: _demo_security_check
login_path: _demo_login
logout:
path: _demo_logout
target: _demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
# http://symfony.com/doc/current/book/security.html#security-book-access-control-matching-options
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/activity/main, roles: IS_AUTHENTICATED_ANONYMOUSLY}
# - { path: ^/activity/new, roles: ROLE_USER }
- { path: ^/activity, roles: ROLE_USER}
的routing.yml
comtwclagripayroll:
resource: "@comtwclagripayrollBundle/Controller/"
type: annotation
prefix: /
Activity1Repository
<?php
namespace com\twcl\agripayrollBundle;
use Doctrine\ORM\EntityRepository;
class Activity1Repository extends EntityRepository
{
public function findOverlappingWithRange(\DateTime $startDate, \DateTime $endDate)
{
$qb = $this->createQueryBuilder('e');
$expr1 = $qb->expr()->andX('e.startDate < :endDate AND e.endDate > :startDate');
$expr2 = $qb->expr()->andX('e.endDate > :startDate AND e.startDate < :endDate');
$orExpr = $qb->expr()->orX($expr1, $expr2);
return $qb->andWhere($orExpr)
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->getQuery()
->execute()
;
}
}
ActivityType
<?php
namespace com\twcl\agripayrollBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ActivityType extends AbstractType {
protected $employeeList;
protected $formstate;
public function setEmployeeList($emplist) {
$this->employeeList = $emplist;
}
public function getEmployeeList() {
return $this->employeeList;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('payrollperiodid', 'entity', array(
'label' => 'Payroll Period',
'class' => 'comtwclagripayrollBundle:Payrollperiod',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.startDate', 'ASC');
// ->where('p.state=1');
}
)
)
->add('activitydate', 'datetime', array(
'label' => 'Activity Date',
/* 'data' => new \DateTime() */ )
)
->add('empsearch', 'text', array('mapped' => false, 'label' => 'Employee Id', 'required' => false))
->add('empid', 'choice', array('label' => 'Employee', 'choice_list' => $this->getEmployeeList())
)
->add('sectionsearch', 'text', array('mapped' => false, 'label' => 'Section Code', 'required' => false))
->add('sectionid', 'entity', array(
'attr' => array('class' => 'activity_section_id_select_field'),
'label' => 'Section',
'data_class' => null,//'com\twcl\agripayrollBundle\Entity\Section',
'class' => 'comtwclagripayrollBundle:Section',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('s')
->innerJoin('s.farmid', 'f')
->addOrderBy('f.name')
// ->addOrderBy('s.quadrant')
->addOrderBy('s.id')
;
}
)
)
->add('ratecode', 'entity', array(
'label' => 'Rate Code & Unit of Measure',
'class' => 'comtwclagripayrollBundle:Rate',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('r')
->orderBy('r.ratecode');
}
)
)
->add('activitytype', 'entity', array('label' => 'Activity Type', 'class' => 'comtwclagripayrollBundle:Acttype',)
)
->add('unitwork', 'number', array('label' => 'Units Worked', 'required' => true,))
->add('activitydesc', 'text', array('label' => 'Comments', 'required' => false))
->add('versionnumber', 'hidden')
->add('sectionIdOnly', 'hidden')
->add('farmNameOnly', 'hidden');
// ->add('dateentered', 'datetime', array('data' => new \DateTime(), 'read_only' => true))
// ->add('userid');
// $builder->addEventListener(\Symfony\Component\Form\FormEvents::PRE_SET_DATA, function(\Symfony\Component\Form\FormEvent $event) {
// $activity = $event->getData();
// $form = $event->getForm();
//
//// if (isset($farm)) {
//
// $form->add('sectionIdOnly', 'hidden')
// ->add('farmNameOnly', 'hidden');
//// }
// });
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'com\twcl\agripayrollBundle\Entity\Activity',
'cascade_validation' => true,
));
}
/**
* @return string
*/
public function getName() {
return 'com_twcl_agripayrollbundle_activity';
}
}
答案 0 :(得分:2)
在security.yml
的最后一行中,您告诉Symfony,每个以/activity
开头的网址都要求用户进行身份验证并拥有ROLE_USER
。
如果您未经过身份验证,则会被框架重定向到登录页面。