Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- public function executeAddToCart(sfWebRequest $request)
- {
- $this->form = new CartForm();
- if($this->isMethod('post')&& $this->form->bindAndSave($request->getParameter('cart')))
- {
- $this->getUser()->setFlash('notice','Items have been added to your cart');
- $this->redirect('@payment');
- }
- }
- //in a controller
- public function executeRecentProducts(sfWebRequest $request)
- {
- $this->products = Doctrine::getTable('Product')
- ->createQuery('p')
- ->where('p.is_published=1')
- ->orderBy('published_at DESC')
- ->limit(10)
- ->fetchArray();
- }
- //in a controller
- public function executeRecentProducts(sfWebRequest $request)
- {
- $this->products = ProductTable::getRecents($max = 10,$published = true);
- }
- //1:
- class myWebRequest extends sfWebRequest
- {
- public function getHttpRowPostData()
- {
- if(!this->isMethod('post'))
- {
- return;
- }
- return trim(file_get_contents('php://input'));
- }
- }
- //2
- //in a controller
- public function executeProcess(myWebRequest $equest)
- {
- $postContent = $request -> getHttpRowPostData();
- //...
- }
- //3
- // #factories.yml
- /*
- all:
- request:
- class: myWebRequest
- */
- //in the template
- /*
- <?php if ($sf_request->hasParameter('foo')); ?>
- <p class="ok"> Hax foo: <?php echo $sf_request->getParameter('foo') ?></p>
- <?php else: ?>
- <p class="ko"> Haz't foo</p>
- <?php endif;?>
- <?php if(count($foo)); ?>
- <?php foreach ($foo as $bar): ?>
- <p><?php echo $bar ?></p>
- <?php endforeach; ?>
- <?php endif;?>
- */
- class invoicesActions extends sfActions
- {
- /**
- * List all avaliable invoices
- *
- *@param sfWebRequest $request The HTTP request
- */
- public function executelistInvoices(sfWebRequest $request)
- {
- $this->invoices = InvoicePeer::retrieveAll($request->getParameter('type'));
- }
- }
- //in a controller
- public function executeMakeSandwich()
- {
- try
- {
- $this->sandwich =SandwichMake::makeSandwich();
- $this->getUser()->setFlash('notice','Your sandwich is ready');
- }
- catch(HomeException $e)
- {
- $this->logMessage('Problem with ham: '. $e->getMessage(), 'err');
- $this->getUser()->setFlash('error','Can\t make the sandwich,try with sude');
- }
- $this->redirect('@make_sandwich');
- }
- protected function buildQuery()
- {
- $tableMethod = $this->configuration >getTableMethod();
- if(is_null($this->filters))
- {
- $this->filters = $this->configuration->getFilterForm($this->getFilters());
- }
- $this->filters->setTableMethod($tableMethod);
- $query = $this->filters->buildQuery($this->getFilters());
- $this->addSoftQuery($query);
- $event = $this->disatcher->filter(new sfEvent($this, 'admin.build_query'),$query);
- $query = $event->getReturnValue();
- return $query;
- }
- //in a controller
- public function executeMakeCoffe(sfWebRequest $request)
- {
- $this->getUser()->makeCoffe($request->getParmeter('beans', array()));
- }
- //in myUser (or any sfUser derivated class)
- public function makeCoffe($beans)
- {
- $coffeeDrops = array();
- foreach($beans as $bean)
- {
- $coffeeDrops[] = CoffeeMaker::mill($bean)->infuse();
- }
- $this->setAttribute('coffee', implode('~',$coffeeDrops));
- }
- // in a controoler
- public function executeRecentlySeenProducts(sfWebRequest $request)
- {
- $this->products = Doctrine::getTable('Product')
- ->createQuery('p')
- ->select('p.id')
- ->where('is_published=1')
- ->limit(10)
- ->fetchArray(); //array(1,2,3)
- $this->getUser()->setAttribute('recentlySeenProducts',$this->products);
- }
- //1
- //the model
- class Story extends BaseStory
- {
- protected $sf_user= null;
- public function __construct(sfUser $sf_user)
- {
- $this->sf_user = $sf_usre;
- }
- public function save($con = null)
- {
- if($this->sf_user instanceof sfBasicSecurityUser)
- {
- $this->created_by = $this->sf_user->getId();
- }
- return parent::save($con);
- }
- }
- //2
- //the controller
- class StoryAction extens sfAction
- {
- public function executeCreate(sfWebRequest $request)
- {
- $story = new Story($this->getUser());
- $story->fromArray($request->getParameter('story'));
- $story->save();
- }
- }
- // the model
- class Story extends BaseStory
- {
- }
- //the controller
- class sstoryAction extends sfAction
- {
- public function executeCreate($request)
- {
- $story = new Story();
- $story->fromArray(array_merge($request->getParameter('story',array()), array('created_by'=>$this->getUser()->getId())));
- $story->save();
- }
- }
- //factories.yml
- /*
- dev:
- logger:
- class: sfAggregateLogger
- param:
- level: debug
- loggers:
- sf_web_debug:
- class:sfWebDebugLogger
- param:
- level:debug
- condition: %SF_WEB_DEBUG
- xdebug_logging: false
- web_debug_class:sfWebDebugForSf12
- */
- //1
- //in a controller
- public function executeList(sfWebRequest $request)
- {
- $this->posts = PostTable::getRecents(sfConfig::get('app_blog_max_items',10));
- }
- //2
- //app.yml file
- /*
- all:
- blog:
- max_items:10
- */
- //#routing.yml
- /*
- homepage:
- path: /{_locale}
- ticket_index:
- path: /{_locale}/tickets
- tickets_detail:
- path: /{_locale}/tickets/{id}
- tickets_create:
- path: /{_locale}/tickets/create
- login:
- path: /login
- //#routing.yml
- homepage:
- path: /{_locale}
- requirements: {_locale: "(nl|fr|en)"}
- ticket_index:
- path: /{_locale}/tickets
- requirements: {_locale: "(nl|fr|en)"}
- tickets_detail:
- path: /{_locale}/tickets/{id}
- requirements: {_locale: "(nl|fr|en)", id: "[1-9][0-9]*"}
- tickets_create:
- path: /{_locale}/tickets/create
- requirements: {_locale: "(nl|fr|en)"}
- login:
- path: /login
- //#services.yml
- parameters:
- available_locales: [nl,fr,en]
- services:
- check_locale_listener:
- class: CheckLocaleListener
- arguments: [%available_locales%]
- tags:
- - name: kernel.event_listener
- event: kernel.request
- method: checkLocale
- priority: 24
- */
- //1 doan trong php
- namespace KingFoo\Presentation\Controller\BlogPost;
- use Doctrine\Common\Presistence\ObjectManager;
- use Symfony\Componet\HttpFoundation\Request;
- class CreateController
- {
- public function _construct(ObjectManager $manager)
- {
- //...
- }
- public function _invoke(Request $request)
- {
- //...
- }
- public function _invoke1(Request $request)
- {
- if(!this->authorizationChecker->isGranted('ROLE_EDITOR'))
- {
- throw new AccessDeniedHttpException();
- }
- $response = new Response();
- $response->setMaxAge(3600);
- return $this->templating->renderResponse(
- 'blog_post/create.html.twig',
- array(
- //view parameters
- ),
- $response
- );
- }
- }
- //ghe
- use Sensio\Bundle\FrameworkExtraBundle\Configration\Cache;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- /**
- *@Route(service="blog_post_crete_controller")
- */
- class CreateController
- {
- /**
- *@Route("/blog/create", name="foo")
- *@Cache(maxege=3600)
- *@Security("has_role('ROLE_EDITOR')")
- *@Template("blog_post/create.html.twig")
- */
- public function _invoke(Request $request)
- {
- return array(
- //view parameters
- );
- }
- }
- //nghi la su dung 3 thu vien nya
- use SymfonyComponentHttpFoundationCookie;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentHttpFoundationStreamedResponse;
- class BetterController
- {
- public function __invoke(Request $request)
- {
- $template = $request->query->get('template');
- $response = new StreamedResponse(
- function() use ($template)
- {
- $this->archaicPdfLib->output($template);
- }
- );
- $cookie = new Cookie('lastGeneration', time());
- $response->headers->setCookie($cookie);
- return $response;
- }
- }
- //security.yml
- //access_control:
- // -{path: ^/(nl|fr|en)/admin, roles:ROLE_ADMIN}
- //
- class AdminController
- {
- /*
- *@Route("/{_locale}/admin")
- *@Security("has_role('ROLE_ADMIN')")
- */
- public function _invoke(Request $request)
- {
- //if (!this->authorizationChecker->isGranted('ROLE_ADMIN')){
- // throw new AccessDeniedHttpException();
- // }
- }
- }
- //use Bcrypt
- #security.yml
- /*
- security:
- encoders:
- KingFoo\Presentation\Security\user:
- algorithm: bcrypt
- cost:13
- security:
- password_encoder:
- class: Symfony\Component\Security\...\BCryptPasswordEnoder
- arguments: [13]
- security:
- encoders:
- KingFoo\Presentation\Security\user:
- id: password_encoder
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement