1
votes

I have an application on my own computer (Kubuntu 13.10).

When I use separate class to define routes everything works perfect. For instance, I have url backend/dictionaries which handled by module backend, DictionariesController and indexAction. View for this url was at views/dictionaries/index.phtml.

Then I've switch to Phalcon\Mvc\Router\Annotations() and mount module's routes like $router->addModuleResource('backend', 'MyApp\Backend\Controller\Dictionaries');

Starting from that point my view does not rendered until I rename views/dictionaries to views/Dictionaries, with capital D.

However, when I've transfered project to production server (Debian 7) to see my views I had to rename view-folder back to lowercase.

So, on my computer action view lays in Controllername/acitonname.php (Dictionaries/index.php, for example) but on production it should be controllername/actionname.php (dictionaries/index.php)

I've printed controller name from dispatcher - on my machine it starts with capital letter but on production it is lowercase.

The question is why it is happend and how it can be fixed without handling 'dispatch:beforeDispatchLoop'?

ADDED:

The problem with development and production was in different phalcon version. On dev I use 1.2.4, on production - 1.2.3

But there are still bug (or feature) with route vs annotation routing definition. If I use annotation the controller name is Capitalized whereas if I use something like $route->add('/:controller/:action') definition the controller name is lowercase.

1
Have you been playing with view cache? Try to set $this->view->cache(false); in your action and see if it works. - Maciej Sz
No, I did not use cache. At least I did not initialize it. - vansanblch

1 Answers

2
votes

You face a century old problem: works at localhost but not on server. The solution is simple as use server environment for development.

For example I have 3 computers, one is Mac, one Windows and one God knows what. One day Ubuntu, another kubuntu or any other fancy named OS. If your server is Debian 7, no matter what OS you use at your working machine, your code always must be handled by Debian 7 and PHP configuration should be as close as your real machine's. Then you will not have such bugs like different paths to file, different end of line symbols and some other crazy stuff.

I suggest you trying Vagrant form https://www.vagrantup.com/downloads.html

You steps are like this:

  1. Download https://www.virtualbox.org and install
  2. Download https://www.vagrantup.com/downloads.html and install
  3. Configure vagrant to your needs. Debian 7 with some php extensions or whatever you have at your production server.
  4. Just navigate with terminal, command line or windows power shell to the directory where your vagrant file is and type "vagrant up --provision"
  5. edit your main OS hosts file (on most linux should be here: /etc/hosts) add line: xxx.xxx.xx.xxx domain.vm (xxx.xxx.xxx.xxx - your virtual box ip address from vagrant config file. domain.vm is your domain which will work for your local development. enter anything you wish, i just like to use .vm tld for virtual machine)

now vagrant will read your config file, download needed virtual box, install php, keep in sync your main OS's folder with virtual box folder. You will edit your files as usual, vagrant will make sure, that on virtual box you have same files.

you should be able to go to http://domain.vm/ and see your site running on virtual machine with debian 7 locally on your kubuntu, windows, mac... OS. And what is the best thing - you don't have to worry about difference between OSs you use locally and on server.

Also if you would add all these things to git versioning, you would be able to work with any computer with your project under ±15 minutes (depends on internet speed to download 1 time virtual machine OS).

Maybe I should make a video tutorial on all this thing.. But you should be able to find one on Youtube.

Just to make your life easier, here's my working Vagrant config. I use ubuntu + PhalconPHP framework + MongoDb and Mysql. There are 2 files. One for vagrant main things: download OS, mount sync folder etc, and one - for installation after starting vagrant. If you want to rerun installation script, just type vagrant provision

Vagrantfile file (name of the file is just "Vagrantfile")

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Base Box
  # --------------------
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  # Connect to IP
  # --------------------
  config.vm.network :private_network, ip: "192.168.5.0"

  # Forward to Port
  # --------------------
  #config.vm.network :forwarded_port, guest: 80, host: 8080

  # Optional (Remove if desired)
  config.vm.provider :virtualbox do |v|
    # How much RAM to give the VM (in MB)
    # -----------------------------------
    v.customize ["modifyvm", :id, "--memory", "700"]

    # Uncomment the Bottom two lines to enable muli-core in the VM
    #v.customize ["modifyvm", :id, "--cpus", "2"]
    #v.customize ["modifyvm", :id, "--ioapic", "on"]
  end

  # Provisioning Script
  # --------------------
  config.vm.provision "shell", path: "init.sh"

  # Synced Folder
  # --------------------
  config.vm.synced_folder "./", "/var/www/", :mount_options => [ "dmode=775", "fmode=644" ], :owner => 'www-data', :group => 'www-data'

end

and the installation file (name of the file is "init.sh"):

#!/bin/bash
# Using Precise32 Ubuntu
# to use closest ubuntu mirror by geographic location
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-updates main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-backports main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-security main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list

sudo apt-get update
sudo apt-get update

#
# For PHP 5.5
#

sudo apt-get install -y python-software-properties
sudo add-apt-repository ppa:ondrej/php5
sudo apt-get update

#
# MySQL with root:<no password>
#
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server

#
# PHP
#
sudo apt-get install -y php5 php5-dev apache2 libapache2-mod-php5 php5-mysql php5-curl php5-mcrypt php5-gd php5-imagick

#
# Redis
#
sudo apt-get install -y redis-server

#
# MongoDB
#
sudo apt-get install mongodb-clients mongodb-server

#
# Utilities
#
sudo apt-get install -y curl htop git-core gcc autoconf
sudo apt-get install -y libpcre3-dev

#
# Redis Configuration
# Allow us to Remote from Vagrant with Port
#
sudo cp /etc/redis/redis.conf /etc/redis/redis.bkup.conf
sudo sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf
sudo /etc/init.d/redis-server restart

#
# MySQL Configuration
# Allow us to Remote from Vagrant with Port
#
sudo cp /etc/mysql/my.cnf /etc/mysql/my.bkup.cnf
# Note: Since the MySQL bind-address has a tab character I comment out the end line
sudo sed -i 's/bind-address/bind-address = 0.0.0.0#/' /etc/mysql/my.cnf

#
# Grant All Priveleges to ROOT for remote access
#
mysql -u root -Bse "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION;"
sudo service mysql restart



#
# Composer for PHP
#
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

#
# Apache VHost
#
cd ~
echo '<VirtualHost *:80>
        DocumentRoot /var/www/public
        SetEnv APPLICATION_ENV "development"
</VirtualHost>

<Directory "/var/www/public">
        Options Indexes Followsymlinks
        AllowOverride All
        Require all granted
</Directory>
ErrorLog /var/www/logs/error.log
' > vagrant.conf

sudo mv vagrant.conf /etc/apache2/sites-available
sudo a2enmod rewrite

#
# Install PhalconPHP
# Enable it
#
cd ~
git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install

echo "extension=phalcon.so" > phalcon.ini
sudo mv phalcon.ini /etc/php5/mods-available
sudo php5enmod phalcon
sudo php5enmod curl

#
# Install PhalconPHP DevTools
#
cd ~
echo '{"require": {"phalcon/devtools": "dev-master"}}' > composer.json
composer install
rm composer.json

sudo mkdir /opt/phalcon-tools
sudo mv ~/vendor/phalcon/devtools/* /opt/phalcon-tools
sudo ln -s /opt/phalcon-tools/phalcon.php /usr/bin/phalcon
sudo rm -rf ~vendor


#
# PHP.ini params edits
#
sudo echo "; ######### PHP.ini modifications from vagrant init.sh #######" >> /etc/php5/apache2/php.ini
sudo echo "error_reporting = E_ALL | E_STRICT" >> /etc/php5/apache2/php.ini
sudo echo "display_errors = On" >> /etc/php5/apache2/php.ini

#
# Reload apache
#
sudo a2ensite vagrant
sudo a2dissite 000-default
sudo service apache2 reload
sudo service apache2 restart
sudo service mongodb restart

#echo -e "----------------------------------------"
#echo -e "To create a Phalcon Project:\n"
#echo -e "----------------------------------------"
#echo -e "$ cd /var/www"
#echo -e "$ phalcon project projectname\n"
#echo -e
#echo -e "Then follow the README.md to copy/paste the VirtualHost!\n"

#echo -e "----------------------------------------"
#echo -e "Default Site: http://192.168.5.0"
#echo -e "----------------------------------------"

####### writable Volt directory
sudo mkdir /vagrant/cache/volt/
sudo chmod 777 /vagrant/cache/volt/

Of course if you need Debian 7, installation script should be adapted to your needs and edit Vagrantfile lines about ubuntu to your OS. Here is the list of all supported OS's: http://www.vagrantbox.es

Change these two lines in Vagrant file:

  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

And also change install script, i don't use Debian, can't tell what can go wrong.