Thursday, June 13, 2013

Steps to move Symfony 2 project to hosting?

Steps to move Symfony 2 project to hosting?

 

Altenative 1

First the httpd.conf file MUST be changed to reference /sites/dir/website/web directory as the DocumentRoot directory, all other files such as lib and config must not be accessible over the web.

 

Altenative 2

 

  1. It depends on kind of hosting that you have: if you have SSH console, then you can do it on hosting after step 2, if you haven`t than do it localy: run command 'php app/console cache:clear --env=prod'.
  2. Suppose you have on you hosting folders youdomain/public_html, so in public_html must be located all web files. So you must upload all from symfony project (folders: app,src,vendors, bin; files: deps, deps.lock), except folder 'web' in folder 'youdomain'. Everuthing from folder 'web' upload to folder public_html.
  3. Check CHMOD for folders app/cache and app/logs, there should be write access.
  4. If there is no file .htaccess in public_html, then create it and add such code in it: https://raw.github.com/symfony/symfony-standard/master/web/.htaccess
  5. Now you should use youdomain.com/index instead of youdomain.com/app_dev.php/index, that you use locally. If site still did not works, you can open file web/config.php and find code where perform check for IP, you find there only ip 127.0.0.1, add your current ip to this list and upload new config on server. Then you can open path yourdomain/config.php and check what`s wrong. If config.php shows that everything ok, but still didn't work, you can enable app_dev.php to debug: open app/app_dev.php and your ip as for config.php. Now you can run scripts as localy using app_dev.php.

 

 

Alternative3

 

I got it to work, this is my experience:
  • Upload the whole project folder to the server.
  • Enter www.your-website.com/project-name/web/config.php.
  • It should say: "This script is only accessible from localhost".
  • Open this web site: http://www.whatismyip.com, it should show you your public IP address, copy it.
  • Open the config.php from the admin panel (like cPanel) and edit that config.php:

if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', /*your IP here*/))) {
    header('HTTP/1.0 403 Forbidden');
    die('This script is only accessible from localhost.');  
}

  • Refresh your config.php file, the page will tell you if your system is missing some required conditions like: PHP version, APC extension, giving /cache and /log folders permissions to write on, etc.
  • After you provide the required conditions you'll see a form of configuring you project to connect to a database if you have one, this step is pretty simple.
  • Open the link www.your-website.com/project-name/web/app_dev.php, it'll help you get started with Symfony2 project.
  • In case you got in app_dev.php this message: You are not allowed to access this file... just do the same thing to app_dev.php as you did in steps 4 and 5 (add your public IP address to the array).
    Note of Hakan Deryal (comment): If you don't have a fix IP address, you need to do this last step each time you get a new IP adrdress from the DHCP. So to solve that, open the app_dev.php and comment out the line die('You are not allowed to.., however this is not a recommended way because you're disabling the built-in security of the file.
  • One thing stopped me and may stop you too, the server I deployed the project on, was case-sensitive (unlike the localhost on my computer), so it kept telling me that the template (Index.html.php for example) does not exist, however it does exist, but I did return $this->render('...:index.html.php') with small i in DefaultController.php. So render the exact template (file) name with the same letters cases.
Now everything is going well, I hope that helps you.

 

Thursday, June 6, 2013

Paginator Bundle :: KnpPaginatorBundle

1. Installation
    -add to composer.json

{
    "require": {
        "knplabs/knp-paginator-bundle": "dev-master"
    }
}
 
 
2. configure
  - add app/config/config.yml
 
knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
 
 
  3. Add to application kernel
   - add  app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        // ...
    );
} 
 
 
 
 
 
The controller :
 
 
        $em    = $this->get('doctrine.orm.entity_manager');
        $dql   = "SELECT a FROM ASFPembedaBundle:Quran a WHERE a.sura = '$surah'";
        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
           $query,
           $this->get('request')->query->get('page', 1)/*page number*/,
           10/*limit per page*/
        );


        return $this->render('ASFPembedaBundle:Quran:qsurah.html.twig', array(
            'pagination' => $pagination,
            'comments'  => 'ssssss'
        ));
 
 
The view :
 
   <table border=1>
   {# table body #}
   {% for ayat in pagination %}
         <tr {% if loop.index is odd %}class="snippet"{% endif %}>
            <td>{{ ayat.aya }}</td><td>{{ ayat.text }}</td>
         </tr>
         <tr {% if loop.index is odd %}class="grey"{% endif %}>
            <td></td><td>
            <div>
               <p>{{ ayat.quranmakna.mtext }}</p>
            </div>
            </td>
         </tr>
         
    {% else %}
        <p>Search text not found</p>
    {% endfor %}
</table>
   
{# display navigation #}
<div class="navigation">
    {{ knp_pagination_render(pagination) }}
</div> 
 

Unknown database type enum requested ...

Installing the FOSuser and get error after the below step command :

php app/console doctrine:schema:update --force


[Doctrine\DBAL\DBALException]                                               
  Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform 
   may not support it.


doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]


vi app/config/config.app

add :

mapping_types:
    enum: string
 
 
to the doctrine:dbal:
 
like this : 
 
 
doctrine:
    dbal:
        driver: %database_driver%
         host: %database_host%
         port: %database_port%
         dbname: %database_name%
         user: %database_user%
         password: %database_password%
         charset: UTF8
         mapping_types:
             enum: string 

Wednesday, June 5, 2013

Login Bundle :: FOSUser

ref : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#LNaN

Install login bundle :

Step 1:  edit composer.json

{
    "require": {
        "friendsofsymfony/user-bundle": "~2.0@dev"
    }
}

Step 2:  download
php composer.phar update friendsofsymfony/user-bundle
 

Step 3:  Enable bundle, edit app/AppKernel.php
 
<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\UserBundle\FOSUserBundle(),
    );
}
 
  
Step 4:  Creating User Class

<?php 
// src/ASF/PembedaBundle/Entity/User.php

namespace ASF\PembedaBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}
 
 
Step 5: Configure your application's security.yml
 
 
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN } 
 
 
Step 6: Configure the FOSUserBundle
 
 # app/config/config.yml
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: ASF\PembedaBundle\Entity\User
 
Step 7: Import FOSUserBundle routing files



# app/config/routing.yml
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile


Step 8: Update your database schema


php app/console doctrine:schema:update --force
 
You now can login at http://symfony/app_dev.php/login