wasil.org Abandon the search for Truth; settle for a good fantasy

25Oct/121

Zend Framework 1 and Composer

No Gravatar

Recently I found out, that Zend Framework 2 fully supports Composer - gem like package manager for PHP. But I couldn't find a way to use it with Zend Framework 1 because of it's default Zend_Loader_Autoloader behavior. I really wanted to use it so I came up with dirty hack, here it is:

  1. first you have to add require 'vendor/autoload.php'; in order to load Composer autoloader. I do it in Bootstrap.php file:
    (By the way as you can see I'v put vendor folder in library, just to keep things organized.)

    /** *| loading Composer autoloader. */ 
     protected function _initComposer() 
     { 
      require '../library/vendor/autoload.php'; 
     }
    

  2. now we have (theoretically) access to libraries provided by Composer, but we can't load them, because Zend_Loader_Autoloader is in our way, trying to do it's thing and load classes from appropriate folder structure. We have to bypass it, by turning it off, accessing what we need from Composer, and turning it on again, so it will be available for further auto loading.

  3. switching off:

    spl_autoload_unregister(array('Zend_Loader_Autoloader','autoload'));

  4. now some loading happens, for example:

    $phpExcel = new PHPExcel();

  5. and now in order to bring it back:

    spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload'));

I know it's dirty, but works for me, and actually performs really well :)
Fork me on GitHub
2Aug/121

Sublime Text 2 perfect PHP development set-up

No Gravatar

Lately, I've become a huge fun of Sublime Text 2, which in my humble opinion (OK, not only mine, take a look in here) is best text editor/IDE I've ever used.

I called it IDE (Integrated Development Environment) which isn't really true yet, but it's getting awfully close to become one.

Sublime Text 2 is great on its own, but it really starts to kick ass with help of addons called 'packages'. You can look for them on Internet or browse on wbond website.

OK, but getting to the point: I use ST2 for some time now for PHP development and I tried almost all packages I came across which are even slightly related to my job. In order to get most of them (except one or two) you'll need wbond's Sublime Package Control. Ok, really you CAN install all of them manually, but what's the point of re-inventing the wheel? I'll stick with the easy way if you don't mind ;)

And here is my list, enjoy and maybe throw a suggestion or two if you know something I don't know ;)

  1. SublimeCodeIntel (https://github.com/Kronuz/SublimeCodeIntel)
    This is package I use all the time. It's job is to provide hints related to function, object, variable etc. names. It also hints what methods and parameters are available in objects/classes. It's based on Komodo CodeIntel and sometimes it has some problems working at all. But most of the time it rocks :)
  2. SublimeLinter (https://github.com/SublimeLinter/SublimeLinter)
    This package does full-time job for me - it constantly watches what I write and instantly lets me know if something is not OK, for example I forget semi-colon or brackets.
  3. SFTP, paid (http://wbond.net/sublime_packages/sftp)
    Great package enabling very comfortable use of SFTP, FTP and FTPS. It supports remote folders browsing, editing and synchronization between local and remote files. Also regular upload, download pn save or file open.
  4. SideBarGit (https://github.com/SublimeText/SideBarGit)
    I use Git on daily basis and having an easy and comfortable GUI is essential. This is what this package does - it provides set of most often used functions and integrates it with ST2 via right-click menu.
  5. Tortoise (on Windows only) (http://wbond.net/sublime_packages/tortoise)
    I use it as a complementary package to SideBarGit if I'm working in Windows environment. It doesn't have so many features but those what it provide are very helpfull and intuitive.
    If you like Tortoise Git but you are working on Linux you might want to check out RabbitVCS. It isn't integrated with ST2, but it provides comfortable right-mouse-click Git GUI.
  6. sublime-github (https://github.com/bgreenlee/sublime-github)
    Every developer should use code snippets of some kind. I use Gist provided by GitHub and this package enables me to use them instantly (save/paste).
  7. Tag (https://github.com/SublimeText/Tag)
    Great package, which simply takes care of all tag-matching related issues. Love it :)
  8. DocBlockr (https://github.com/spadgos/sublime-jsdocs)
    Nobody likes it, at least nobody I know, but as a developer you have to maintain solid code documentation. This is where this package helps - it prepares DocBlock comments for classes, params, methods etc. Speeds up things.
  9. Phpcs (http://soulbroken.co.uk/code/sublimephpcs)
    This package adds support for PHP_CodeSniffer to ST2. It checks if you code is written according to one of the standards. Just pick one: Zend, PEAR.. and more :) Check out PHP_CodeSniffer website for configuration details.
  10. TrailingSpaces (https://github.com/SublimeText/TrailingSpaces)
    Small horror of every code - empty spaces at lines ends, at the end of file. Especially troublesome if you are working on the code with other developers, who might use different editors/IDEs which might interpret them (empty spaces) differently.
  11. BracketHighlighter (https://github.com/facelessuser/BracketHighlighter)
    One of my favorites: clearly highlights matching brackets, quotes etc. Just makes everyday life easier when looking at code with multiple brackets-in-brackets constructions.
  12. DetectSyntax (https://github.com/phillipkoebbe/DetectSyntax)
    Detects syntax of opened file. ST2 doesn't always get it right and this packages helps.
  13. EncodingHelper (https://github.com/SublimeText/EncodingHelper)
    Detects and displays file encoding and sticks to it. Helpful in shared development environment if someone uses for example IDE with default windows encoding instead of UTF-8.
  14. Sublime Function Name Display (https://github.com/akrabat/SublimeFunctionNameDisplay)
    Displays in bottom bar name of a function (or method) in which our cursor is placed. Simple and helpful.
  15. Terminal (http://wbond.net/sublime_packages/terminal)
    Using Git or maybe some PEAR packages you might need to switch to console from time to time and do some magic. Maybe use Zend Tool or "bake" something ;) It's just what this plugin is for.
  16. Xdebug (https://github.com/Kindari/SublimeXdebug)
    Last but not least - power of Xdebug in ST2. It brings Xdebug capabilities to ST2 and enables debugging as most of "big" IDEs can do.

Few packages I use but don't consider essential:

  1. Color Highlighter (https://github.com/Monnoroch/ColorHighlighter)
    When you move your cursor on color definition (hex, for example) it highlights text with this color. helpful when writing CSS.
  2. ColorPicker (https://github.com/weslly/ColorPicker)
    Graphical color picker. Doesn't really need explanation, doesn't it?
  3. Prefixr (http://wbond.net/sublime_packages/prefixr)
    This one is nice and I would probably make more use of it if I were developing more CSS :)
  4. All Autocomplete (https://github.com/iamnoah/Autocomplete-All)
    Enhances ST2 ability to autocomplete by increasing scope to all opened files, not only one we are currently in.
  5. ApacheConf.tmbundle (https://github.com/colinta/ApacheConf.tmLanguage)
    Language definitions for Apache conf and .htaccess files.
  6. Zend-Framework.tmbundle (https://github.com/ybits/Zend-Framework.tmbundle)
    Code snippets for developing in Zend Framework. Although I do use ZF I never started to make use of these snippets, at least not of their full potential.
  7. Additional PHP Snippets (https://github.com/stuartherbert/sublime-phpsnippets)
    Same here as with Zend Framework snippets. I might some day start to use more of them.

There are also packages for WordPress, ZenCoding or one moving SemiColon out of brackets and many more, but I'm doing all right without them.

13Jun/120

Zend Framework: options config file

No Gravatar

Zend Framework series #1

How to use options from additional config ini file? Pretty easy actually :)

  1. create new file in application\configs directory. Name it something like extra_parameters.ini
  2. you can place here any type of options and I mean ANY:
    1. list of access levels of your app
    2. information if debug is enabled (or other flag you wish to use)
    3. or any of available Zend config parameters
    4. for example:
      colors[] = blue
      colors[] = red

      or

      resources.session.name = "my_session"

      and so on, and so on... :)

  3. As you might already know Zend Framework recognizes various Application Environments, mostly used are Production and Development, but you might want to use few of your own. Each set of options in INI file must belong to one of these environments. you can define it like this:
    [production]
    ; some options
    [development : production]
    ; some more options

    By the way: you can define that one environment will inherit it's options from another, in my example "development" inherit's from "production".

  4. Now, that you have your options and parameters, it's time to get to them from our application. I will access my config from controller. It is basically few lines of code and it gives you full (read) access to config file, as follows:
    $options = new Zend_Config_Ini('../application/configs/extra_parameters.ini', APPLICATION_ENV);

    APPLICATION_ENV - this will make Zend read only appropriate section from config

    now in order to access particular option do something like this (if it is an array):

    $some_option = $options->your_option->toArray();

    or like this:

    $some_option = $options->your_option;
  5. And that's pretty all. From now on you can use these values as any other PHP variables/arrays, maybe pass them to the view... you options are limitless ;)
20Dec/1146

GitLab installation on Fedora 16 (with gitolite)

No Gravatar
This is updated version of my tutorial for GitLab 2.1. Old version can be found here

GitLab is a wonderful piece of software made by these guys and I'd like to try and install it on Fedora 16 system, as it is most recent Fedora OS at the time I am writnig this. I will base my tutorial on on found here by Déja. Also on my way to successful install I've stumbled upon this description, so I might have gain some experience from it too.

GitLab is Ruby on Rails application and it takes some experience to get it all working because of many, many, many dependencies and packages it needs. It's nothing like simple PHP on Apache installation. But it works and it's worth it, I assure you :)

Let's begin! (by the way: I perform all as root user - again: test environment)

small edit: For people who only want this to work and not necessarily wish to know how and why, I have prepared easy installation scripts in here
another EDIT: GitLab is under heavy development and now it's stable version is 2.1. This means that my guide might be not entirely accurate. Please be cautious. I'll update it soon.
  1. First of all: I am working on virtual machine with brand new and fresh Fedora 16 installed. Minimal installation, just to make sure nothing messes with GitLab. Consider this a test environment.
  2. Once you have your OS ready, update it with yum update and also install some packages you will need on the way:
    yum install make openssh-clients gcc libxml2 libxml2-devel \ 
    libxslt libxslt-devel python-devel
  3. Check what version of ruby is available via yum. GitLab needs at least version 1.9.2
    yum info ruby

    on mine it says that the only available version for me is 1.8.7.357 so I have to get it somewhere else.

  4. Using RVM to install Ruby.
    EDIT: I have switched to ruby 1.9.3p0 - it works and starts a lot faster, you can use it instead of 1.9.2-p290 in this guide.

    I will use RVM, which stands for Ruby Version Manager - brilliant tool, which you can use to install and manage your Ruby with ease. Let's check it out, shall we? (you can install Ruby any way you want - I'll just do this RVM way)

    Notice use of --with-openssl switch - without that passenger will complain about Ruby OpenSSL support and won't compile in next step.

    sudo bash -s stable < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
    
    source /etc/profile.d/rvm.sh
    
    rvm install ruby-1.9.2-p290 --with-openssl
    
    rvm use ruby-1.9.2-p290 --default
    

    Done. Just like that we have operational Ruby 1.9.2-p290. No RPMs, no going back in time.. nice, huh? :)

  5. Good. Small (nice) surprise: our RPM installed ruby gems for us, so now we only have to get rails gem.
    first, just to make sure everything is up to date, execute:
    rvm all do gem update --system

    now install rails with:

    rvm all do gem install rails
  6. Now we will install apache2 as a webserver and Passenger for ruby apps deployment. I know that most GitLab tutorials use nginx but I really like apache ;)
    yum install -y httpd
    rvm all do gem install passenger

    as I mentioned: I use apache, so I will need passenger module for it:
    first some required packages:

    yum install -y gcc-c++ curl-devel openssl-devel zlib-devel \ 
    httpd-devel apr-devel apr-util-devel

    an then use

    passenger-install-apache2-module

    in order to install it.it gives you 2 information on how to proceed with apache configuration:

    • add following lines to /etc/httpd/conf/httpd.conf (at the very end):
      LoadModule passenger_module /usr/lib/ruby/gems/1.9.1/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
      PassengerRoot /usr/lib/ruby/gems/1.9.1/gems/passenger-3.0.11
      PassengerRuby /usr/bin/ruby
      
    • and then create file named default.conf (or gitlab.conf, or something other you like) in /etc/httpd/conf.d/ containing these lines (remember to change to fit your environment):
      <VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public
      <Directory /somewhere/public>
      AllowOverride all
      Options -MultiViews
      </Directory>
      </VirtualHost>
      

      (don't worry, we'll come back to this one later)

  7. Now it is time for gitolite. I won't use gitosis because it is not actively developed any more and I found that new GitLab works better with gitolite.install it with
    yum install -y gitolite

    now, wasn't that quick? :)

  8. Now we will need to create new gitolite server configuration and in order to do that we have to generate SSH key-pair. But first things first.
    To get us started we will create git user with

    adduser -r -m --shell /bin/bash \
     --comment 'git version control' git
    mkdir /home/git/.ssh
    chown git:git -R /home/git/.ssh
    

    Now we have to create SSH keys pair for him:

    ssh-keygen -q -o -N '' -t rsa -f /home/git/.ssh/id_rsa

    To initialize Gitolite we need to first edit it's default configuration file and change default permissions and then initialize it by using git user:

    sed -i 's/0077/0007/g' /usr/share/gitolite/conf/example.gitolite.rc
    
    su - git -c "gl-setup -q /home/git/.ssh/id_rsa.pub"

    as mentioned here

  9. Just to make sure that git user has all necessary access rights to his own home directory after we have added SSH keys for him:

    chown git:git -R /home/git/
  10. OK, time for next requirements: python Pip, I will go with method from original post and NOT use yum for that.
    curl http://python-distribute.org/distribute_setup.py | python
    

    and then

    easy_install pip
    
  11. Database. We need it. We have it already - it was installed on the way as a dependency. Just to make sure check if you have SQLite at list version 3.6 with:
    yum info sqlite

    I have 3.7.7.1. All we need is SQLite development package, get it with:

    yum install -y sqlite-devel
  12. And now the main event! GitLabHQ!

    I like install web apps in /var/www so we will go there and git clone GitLab:

    cd /var/www
    
    git clone git://github.com/gitlabhq/gitlabhq.git
    
    cd gitlabhq/
    

    we need to use pip to install some python dependencies:

    pip install pygments

    and bundler gem so it can later on do most of the work for us:

    rvm all do gem install bundler

    you will need one more dependency for bundle install

    yum install -y libicu-devel

    finally (remember to be in GitLab directory):

    bundle install

    (go get yourself a coffee or other treat - it takes a while)

  13. Redis installation. More about Redis here
    yum install -y redis

    and start it with:

    service redis start
    
  14. Preparing config files.

    copy database and gitlab config files from example to working copies:

    cp config/gitlab.yml.example config/gitlab.yml
    cp config/database.yml.example config/database.yml
  15. DB setup:
    And now database setup and import:

    RAILS_ENV=production rake db:setup
    RAILS_ENV=production rake db:seed_fu
    

    TIP: if you get errors here, check if you don't have 2 versions of rake installed (I had somehow): 0.8.7 and 0.9.2. If you do, remove older with:

    gem uninstall rake

    and choose appropriate version. If you still have problems try:

    gem install rake

    -> don't know why, but if it installs again same version of rake, it works.

  16. Apache virtual host configuration should look something like this (I told you, we will get back to that):file: /etc/httpd/conf.d/gitlab.conf
    <VirtualHost *:80>
    ServerName www.yourhost.com
    DocumentRoot /var/www/gitlabhq/public
    <Directory /var/www/gitlabhq/public>
    AllowOverride all
    Options -MultiViews
    </Directory>
    </VirtualHost>
    
  17. enable apache user, co you can su into this account:
    usermod -s /bin/bash -d /var/www/ apache

    prepare SSH key for him (actually use one created for git user)

    mkdir /var/www/.ssh
    
    cp -f /home/git/.ssh/id_rsa /var/www/.ssh/ && chown apache:apache /var/www/.ssh/id_rsa && chmod 600 /var/www/.ssh/id_rsa
    

    add localhost to apache user known_hosts with:

    ssh-keyscan localhost >> /var/www/.ssh/known_hosts

    also give him permissions to his own home directory with:

    chown -R apache:apache /var/www/
  18. add apache user to git group with:
    usermod -a -G git apache
  19. check if repositories directory has correct permissions, it should be 770. You can set it with
    chmod 770 /home/git/repositories/
  20. double check your firewall (for example iptables) if it allows connections on port 80 to your server.
  21. important: Apache won't be able o start with SELinux set to "enforcing". Please set it to permissive.
  22. start apache server with:
    service httpd start
  23. Now it all should work. Fingers crossed.. go to http://www.yourhost.com
  24. Now you should be able to log in using these credentials:

    user: admin@local.host
    pass: 5iveL!fe

    and you're in :)

It wasn't so hard, wasn't it? ;)

Troubleshooting:

  1. I get error 500 when I enter particular project and try to get into it's "Admin" section. Don't know how to fix it yet.To fix it, check if repositories directory has correct permissions, it should be 770. You can set it with
    chmod 770 /home/git/repositories

    also set this:

    chmod 770 /home/git
  2. If Passenger insists, that we don't have or he can't see grit library (gem actually, as far as I know) even though we have it.

    https://github.com/gitlabhq/grit.git (at master) is not checked out. Please run `bundle install` (Bundler::GitError)

    Only resolution to this problem I have found is to (as root) change /usr/local/rvm/gems/ruby-1.9.3-p0/gems ownership to apache user with:

    chown apache:root -R /usr/local/rvm/gems/ruby-1.9.3-p0/gems

    I know that is not great resolution but it worked for me. If you'll happen to know better way - please let me know, so I can update this tutorial and my knowledge of course ;)

    Edit

    I found another solution (instead of changing permissions): after first

    bundle install

    edit Gemfile and in lines containing "git:" remove everything after first comma, for example:

    gem "grit", :git => "https://github.com/gitlabhq/grit.git"

    should be

    gem "grit"

    and so on.

    then do again

    bundle install

    and now passenger shouldn't complain any more.

24Apr/111

Konwersja statycznej strony HTML do WordPress

No Gravatar

Coraz częściej pojawia się potrzeba konwersji działającej, statycznej strony napisanej w czystym HTML'u lub za pomocą prostych skryptów PHP, do postaci bardziej skomplikowanej, ale dającej dużo większe możliwości. Mam tutaj na myśli systemy CMS ( Content Management System ). Opiszę taką konwersję na przykładzie WordPress'a.

  1. Wstęp
    Typowa statyczna strona jest napisana w HTML'u. Czasem nie jest do końca statyczna, bo ma powklejane tu i tam fragmenty kodu PHP lub JavaScript. Na potrzeby tego tutoriala przyjmę jednak, że mamy do czynienia ze stroną stworzoną za pomocą jedynie HTML'a.
    Taka strona składa się najczęściej z mniej więcej takiej struktury plików, która składa się na całość, którą od tej pory będę w skrócie nazywał template'em: 

    • index.html
    • style.css
    • katalog z grafiką, np.: img

    Plik index.html zawiera całą treść strony, wszystkie tagi HTML oraz odniesienia do lokalizacji pliku z kaskadowymi stylami ( style.css ) oraz lokalizacją grafik. To on jest dla nas najbardziej interesujący i w nim będzie się działa cała "magia" :)

  2. Dla ułatwienia i lepszej wizualizacji konwersji od tej pory będę przedstawiał całą operację na konkretnym przykładzie. Proponuję użycie jako wyjściowego template'a któregoś z dostępnych na stronie Open Source Web Design ( www.oswd.org ) na przykład: LoadFoO [V2]
  3. Po ściągnięciu i rozpakowaniu tego template'a widać takie pliki i katalogi:
    • css  ( katalog zawierający plik style.css - definicję stylów kaskadowych )
    • img ( katalog zawierający pliki graficzne )
    • js ( katalog zawierający pliki JavaScripts )
    • favicon.ico ( plik z ikoną naszej strony pokazującą się w przeglądarce )
    • index.html ( szkielet template'u, tak jak opisywałem wcześniej )
  4. CSS
    Konwencja struktury plików dla template'ów w Worpdress wymaga, aby plik style.css znajdował się w głównym katalogu, tzn tam gdzie index.html , więc przenieśmy go teraz z katalogu css o jeden poziom wyżej. Pusty obecnie katalog css można usunąć, żeby nie zaśmiecał przestrzeni. 

    Plik style.css jest jednocześnie opisem template'a, a konkretnie jego nagłówek, który wygląda np tak:

    /*
    Theme Name: nazwa_template'a
    Theme URI: ścieżka URI
    Description: Opis
    Version: wersja, np: 1.0
    Author: twoje_imię
    Author URI: twój adres WWW
    */

    Umieść ten nagłówek przed wszystkimi definicjami stylów w style.css.

    W naszym przypadku pamiętaj o zmianie ścieżek w pliku css, aby wskazywały na katalog img. Teraz jest to na przykład: ../img/jakis_plik.jpg trzeba je wszystkie zmienić na img/jakis_plik.jpg ( przez usunięcie ../ )

  5. Opcjonalnie, ale zalecałbym to, można stworzyć obraz - miniaturę template'a. Powinien mieć rozmiar 300x225 pikseli oraz format PNG. Nazwa musi brzmieć: screenshot.png i musi byc umieszczony tam gdzie plik style.css.
  6. Cięcie/Gięcie
    Głównym plikiem naszego template'a jest index.php, ale skąd go wziąć? Zmienić rozszerzenie pliku index.html na php
    To jednak nie wystarczy. Trzeba go teraz odrobinę pociąć na kawałki, w najprostszym wypadku będą to pliki:
    header.php - plik zawierający definicje nagłówka strony
    footer.php - plik zawierający stopkę strony
    my dodamy jeszcze sidebar.php 

    Nazwy tych plików są ważne, ponieważ dzięki temu WordPress sam znajdzie te pliki, bez potrzeby ich definiowania. W kolejnych punktach opiszę każdy z nich.

    Plik index.php zawiera jeszcze definicje tak zwanego "loop" czyli takiej "karuzeli", która pokazuje odpowiednie posty/wpisy na stronie głównej. O tym również później.

  7. header.php
    Najpierw przykładowy plik header.php: 

    <html <?php language_attributes(); ?>>
    <head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <title><?phpglobal $page, $paged;

    wp_title( '|', true, 'right' );

    bloginfo( 'name' );

    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
    echo " | $site_description";

    if ( $paged >= 2 || $page >= 2 )
    echo ' | ' . sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );

    ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <?php

    if ( is_singular() && get_option( 'thread_comments' ) )
    wp_enqueue_script( 'comment-reply' );

    wp_head();
    ?>
    </head>

    <body <?php body_class(); ?>>

    Jest on pełny znaczników w języku PHP, które rozpoznaje WordPress po czym wypełnia je zawartością dynamiczną, np:

    <?php bloginfo( 'stylesheet_url' ); ?> opisuje lokalizację pliku style.css ( nie definiujemy nazwy, bo jest on domyślny dla WordPress'a )

    wp_head(); powoduje wyświetlenie nagłówka HTML na stronie
    pozostałe zmienne są w większości przypadków dość jasno opisane przez swoje nazwy.

  8. footer.php
    Przykładowa zawartość pliku footer.php:
    <div id="foot">
    </div>
    </div>
    <?php wp_footer(); ?>
    </body>
    </html> 

    Zawiera on przeważnie definicję stopki zawartej jako jakiś element <div></div> oraz wywołanie PHP: wp_footer(); - zawiera ono w sobie zmienne potrzebne przez wiele wtyczek WordPress'a, głównie odnoszące się do kodu JavaScript.

  9. sidebar.php
    W nim umieścimy zawartość prawej kolumny naszego template'a, czyli w naszym przypadku trzeba wyciąć następujący kod z index.php i wkleić do sidebar.php:
    <ul id="nav">
    <li><a href="#Home">Home</a></li>
    <li><a href="#Archive">Archive</a></li>
    <li><a href="#Link">Links</a></li>
    <li><a href="#download">Download</a></li>
    <li><a href="#support">Support</a></li>
    <li><a href="#contact">Contact</a></li>
    </ul>
    <div>
    <h2 style="margin-top:17px">Recent Entries</h2>
    <ul>
    <li><a href="#">Recent Entries1</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries2</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries3</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries4</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries5</a> <i>01 Des 06</i></li>
    </ul>
    </div>
  10. index.php
    I znowu wracamy do głównego pliku naszego template'a: index.php. Zawiera on w sobie całą resztę kodu naszej strony, oczywiście po wycięciu z niego tych fragmentów, które zawarte są w pozostałych plikach PHP. 

    Nagłówek będzie zdefiniowany w header.php, a więc z index.php można usunąć wszystko od samej góry do znacznika <body> z nim włącznie. Zamiast tego co wytniemy trzeba wstawić: <?php get_header(); ?>

    Kod prawej kolumny już przenieśliśmy do sidebar.php, teraz jest puste miejsce pomiędzy <div id="right"> i </div>. Trzeba je wypełnić kodem PHP wordpress: <?php get_sidebar(); ?>

    Stopkę strony utworzymy poprzez usunięcie wszystkiego poczynając od <div id="foot"> do końca i zastąpienie przez: <?php get_footer(); ?>

    Teraz po otworzeniu panelu sterowania naszej strony WordPress i wejściu w menu Motywy zobaczymy nowy motyw o nazwie jaką mu nadaliśmy, po jego aktywacji zobaczymy nowy template w akcji. Na razie jednak nie wszystko działa tak jak powinno. Na przykład menu, o tym za chwilę.

  11. Menu stron.
    W pliku index.php zmień kod pomiędzy: <div id="menu"><ul> i </ul></div> na kod PHP WordPress: 

    <?php wp_list_pages('sort_column=menu_order&title_li='); ?>

    W tym przypadku dodałem zmienną sort_column=menu_order aby menu korzystało z kolejności jaką mu narzucimy w panelu sterowania oraz title_li=  ( puste), ponieważ nie chcemy aby pokazywał się tytuł "Menu Główne".

    Po odświeżeniu strony zobaczymy już Główne menu zawierające pozycje zdefiniowane przez nas w panelu administracyjnym. Reszta na razie nie jest jeszcze aktywna.

  12. Dodatkowe menu w panelu bocznym.
    Dodanie drugiego menu pozwoli na stworzenie drugiego menu, zawierającego inne elementy niż głowne. Najpierw należy stworzyć plik functions.php o zawartości: 

    <?php add_theme_support( 'menus' ); ?>

    co doda pozycję "Menu" w dziale "Wygląd" w panelu administracyjnym, w którym to miejscu będzie można zdefiniować i dodać kolejne menu do naszej strony.

    W sidebar.php zamień kod:

    <li><a href="#Home">Home</a></li>
    <li><a href="#Archive">Archive</a></li>
    <li><a href="#Link">Links</a></li>
    <li><a href="#download">Download</a></li>
    <li><a href="#support">Support</a></li>
    <li><a href="#contact">Contact</a></li>

    na:

    <?php wp_nav_menu( array( 'sort_column' => 'menu_order') ); ?>

    gdzie sort_column wymusza kolejność w menu taką jak w panelu administracyjnym.

    Dodatkowo, żeby wszystko się tak samo wyświetlało dodaj do style.css następującą linijkę:

    #nav ul {list-style-type:none;list-style-image:none;}

    gdzieś w okolicy definicji #nav

    Po dodaniu elementów z poziomu panelu pojawią się one w wybranym przez nas miejscu w panelu bocznym.

  13. Panel boczny po raz drugi: box
    W dolnej części panelu bocznego znajduje się box zawierający linki do ostatnich wpisów, aby go zrealizować dynamicznie zamień kod: 

    <div class="box">
    <h2 style="margin-top:17px">Recent Entries</h2>
    <ul>
    <li><a href="#">Recent Entries1</a> <i>01 Des 06</i></li>

    <li><a href="#">Recent Entries2</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries3</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries4</a> <i>01 Des 06</i></li>
    <li><a href="#">Recent Entries5</a> <i>01 Des 06</i></li>

    </ul>
    </div>

    na:

    <div class="box">
    <h2 style="margin-top:17px">Recent Entries</h2>
    <ul>
    <?php
    global $post;
    $args = array( 'numberposts' => 5, 'offset'=> 0, 'orderby'=>'post_date');
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :    setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a> <i> <?php the_date(); ?></i></li>
    <?php endforeach; ?>
    </ul>
    </div>

    To spowoduje wyświetlenie linków do 5-ciu ostatnich postów, w kolejności chronologicznej ich publikacji.

  14. Loop
    Jest to taka karuzela, czyli sposób w jaki wyświetlane są posty na głównej stronie, jeśli nie jest ona statyczna.
    Przykładowy kod, jaki użyję w tym template'cie:
    zamiast: 

    <div id="left">
    <h2>Welcome to LoadFoO [V1]</h2>
    <p>Text Content Text <a href="#">Content Text</a> Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content.</p>
    <ul>
    <li>ul-li test.</li>

    <li>ul-li test.</li>
    <li>ul-li test.</li>
    <li>ul-li test.</li>
    <li>ul-li test.</li>
    </ul>
    <h2>Functionality or performance</h2>

    <p>Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content Text Content.
    </p>
    </div>

    użyj:

    <div id="left">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <p> <?php the_content(); ?></p>
    <?php endwhile; else: ?>
    <p>Brak postów.</p>
    <?php endif; ?>
    </div>

  15. Szablon wpisu
    Musimy jeszcze zdefiniować szablon wpisu ( Post ), a robi się to w pliku:
  16. Drobne  poprawki.
    a) W przypadku tego tematu postanowiłem usunąć kod odpowiadający za powiększanie/pomniejszanie tekstu: 

    <div style="float: right;"><a href="javascript:ts('body',1)">[+]</a> | <a
    href="javascript:ts('body',-1)">[-]</a></div>

    b) dodanie tytułu strony w górnej części poprzez zamianę w index.php:

    LoadFoO [V2]

    na:

    <?php bloginfo( 'name' ); ?>

    c) mała zmiana w style.css aby nagłówki wpisów wyglądały tak jak w oryginale:

    dodaj:  h2 a {color: #9F9F9F; text-decoration: none; }

    To by było na tyle, no przynajmniej na początek. Teraz cały dalszy rozwój i życie template'a zależy od Ciebie drogi czytelniku :)

Tagged as: , 1 Comment
8Nov/090

Instalacja Symfony Framework – Debian

No Gravatar

Postanowiłem spróbować szczęścia z Symfony na moim serwerze z systemem Debian Linux, ale okazało się, że to nie jest takie proste. Stąd pomysł, żeby to opisać dla potomnych :)

Założenie: używam Symfony w wersji 1.2 oraz ORM Doctrine a nie Propel

1. najpierw trzeba ściągnąć cały framework ze strony Symfony na swój serwer ( lub tak do, którego masz dostęp z poziomu powłoki ), a następnie:

a) stwórz katalog z nazwą swojego projektu, a następnie w nim stwórz katalog lib/vendor, czyli:

mkdir -p NAZWA_PROJEKTU/lib/vendor

b) rozpakuj plik tgz za pomocą poleceń:

cd lib/vendor
tar zxpf symfony-1.2.9.tgz
mv symfony-1.2.9 symfony
rm symfony-1.2.9.tgz

2. teraz czas sprawdzić, czy mamy zainstalowane całe oprogramowanie wymagane przez Symfony:

cd ../..
php lib/vendor/symfony/data/bin/check_configuration.php

3. i tutaj zaczęły się problemy.

zmiany w php.ini to nie problem, ale trzeba pamiętać, że ( tak jak skrypt nas uprzedza ) plik php.ini, który jest sprawdzany przez skrypt może nie być tym samym plikiem, który "obsługuje" serwer WWW - u mnie właśnie tak było.

Większy problem pojawił się w momencie kiedy chciałem zająć się instalacją PDO, a dokładniej jak chciałem sobie poradzić z komunikatem o braku sterowników do rzeczonego PDO. Potrzebowałem sterownik do mysql, ale szukając przez aptitude nic nie udało mi się znaleźć w pakietach Debian'a. Wyjściem okazało się użycie systemu instalacji dodatków do PHP: pecl.

a) aby z niego skorzystać najpierw należy wykonać polecenie ( jako root ):


aptitude install php-pear

b) a potem zainstalować PDO wraz ze sterownikami do np mysql, lub innymi dostępnymi:

pecl install pdo
pecl install pdo_mysql
pecl install pdo_sqlite

c) potem pozostaje tylko dodanie odpowiednich wpisów do php.ini lub jak w moim przypadku do /etc/php5/pdo.ini:

extension=pdo.so
extension=pdo_mysql.so
extension=pdo_sqlite.so

d) i na koniec restart serwera WWW, np:

/etc/init.d/apache2 restart

To u mnie zadziałało bez problemu z wyjątkiem jednego błędu przy kompilacji pdo_mysql. W trakcie przeprowadzania sprawdzania przed kompilacją okazało się, że nie mam mysql_config i bez tego nie da rady przejść dalej. Aby to naprawić trzeba zainstalować jedną paczkę przez aptitude:

aptitude install libmysqlclient15-dev

i sprawa załatwiona.

Potem dopiero się dowiedziałem, że istnieje o wiele prostszy sposób na instalacje brakujących sterowników do PDO:

aptitude install php5-common php5-mysql php5-sqlite

a samo PDO jest zawarte w pakiecie php5-common

I to by było na tyle: Symfony jest zainstalowane i gotowe do stworzenia aplikacji w tym framework'u.

   
Return to Top ▲Return to Top ▲