...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
March 1, 2012

routing of Zend framework

Question by PalAla

I create a module in zend project and the module has it’s own mvc folders, here’s the structure of the module,

enter image description here
i want to open the index page which located in the view floder of the visit module

here’s the path of the index.phtml

InspectionSysapplicationmodulesvisitsviewsscriptsvisitsindex.phtml

and I try to make routing to the index page in application.ini

resources.router.routes.user.route = /visit
resources.router.routes.user.defaults.module = visits
resources.router.routes.user.defaults.controller = visit
resources.router.routes.user.defaults.action = index

when I type http://localhost/zendApps/InspectionSys/visit it returns 404 error page.

What should I do?

Answer by Starx

Your controller’s name is visits not visit.

Try replacing your route with this

resources.router.routes.user.route = "/visit"
resources.router.routes.user.defaults.module = visits
resources.router.routes.user.defaults.controller = visits
resources.router.routes.user.defaults.action = index

or define your route in bootsrap

 $routeUser = new Zend_Controller_Router_Route(
    '/visit',
    array(
        'module' => 'visits'
        'controller' => 'visits',
        'action' => 'index'
    )
);
$router -> addRoute('visit', $routeUser);

Update 1

The problem seems to be due to the root not being routed to /public.

  1. The proper way: You need to setup a vhost and point the root to the public directory.

  2. Another Way: You need to redirect every request inside public directory. The .htaccess for this file would be

    RewriteRule ^.htaccess$ - [F]
    
    RewriteCond %{REQUEST_URI} =""
    RewriteRule ^.*$ /public/index.php [NC,L]
    
    RewriteCond %{REQUEST_URI} !^/public/.*$
    RewriteRule ^(.*)$ /public/$1
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^.*$ - [NC,L]
    
    RewriteRule ^public/.*$ /public/index.php [NC,L]
    
Read more

$find returning null

Question by Dhaval Shukla

I have radGrid in .ascx page in which I want to find the control using $find but it returns a null to me. Below is my code which I am using to get the object (written in .ascx).

<script type="text/javascript">
    $(function () {
        var Rates_gridID = $find('<%= gridRates.ClientID %>');
        alert(Rates_gridID);
    });
</script>

Here, I am getting Rates_gridID as null in alert. Interesting thing which I noted is when I change the jQuery version to 1.2.6 from 1.6.4 I am getting Rates_gridID object. I have googled this a lot but not getting any solution. I think the problem is with $(function().

Answer by Starx

You are using the incorrect syntax. Try

$("body").find('<%= gridRates.ClientID %>');
Read more

php data classes?

Question by mazzzzz

I’ve learned most of my OOP practices from C#, and coded php primarily in MVC but without custom objects for data types (user information and such I primarily stored in associative arrays). I’m wanting to take what I’ve learned in C# (making structs, or data classes basically, to store specific types of data, such as a user record, or say an article’s information) into php. This is so I can know exactly what data values to expect (because they’re defined fields, instead of just added to an array), allowing a bit more abstraction between controller and view.

Is there a certain way to do this in php, and in the MVC design pattern specifically? I am curious where I should put the definitions of these new “data types.”
Or am I just thinking about this in the wrong way, and there is a better way to do it?

EDIT:
A C# example of what I’m trying to accomplish:

class program1
{
    public void main ()
    {
        Article randomArticle = getArticle (); //instead of returning an object array, it returns a defined data type with predefined fields, so I don't need to guess what's there and what isn't
        Console.WriteLine(randomArticle.title);
        Console.ReadLine();
    }
    public Article getArticle ()
    {
        return new Article("Sometitle", "SomeContent");
    }
}
struct Article
{
    public Title {get; private set;}
    public Content {get; private set;}

    public Article (string title, string content)
    {
        this.Title = title;
        this.Content = content;
    }
}

(idk if the above actually would compile, but it gives you the gist of what I’m attempting to do in PHP)

Answer by zvnty3

In PHP, you are free to write your own systems that accomplish MVC. Instead of rolling your own to start, though, I suggest looking into using an existing system. There’s a huge ecosphere of us php types, and a long history. By some opinions, PHP is more well-established than a younger and faster evolving C#. It’s also simpler, which is nice. Specifically, though: CakePHP is one I’d recommend. Drupal is more robust. And there’s always Zend. Advantage of going Zend is the end-to-end solution from the editor to the server optimization and security.

p.s. c# is more mvvm

EDIT: code example

class Article {

    protected $_title;
    protected $_content;

    public function setTitle( $title ) {
        $this->_title = $title;
    }
    public function setContent( $content ) {
        $this->_content = $content;
    }

    public function getTitle() {
        return $this->_title;
    }
    public function getContent() {
        return $this->_content;
    }

    /* magic not advisable; a technically valid technique, though it can lead to problems
    public function __get( $property ) {
        switch( $property ) {
            case 'title': return $this->_title; break;
            case 'content': return $this->_content; break;
            default: break;
        }
    }
    */

}

Answer by Starx

PHP is free of any logic you would like to implement. IMO, Ensuring data-types leads to a field of validations. I want you to check few terms, that will give you everything required.

  1. Type Casting/ Juggling [docs here]

    Casting is probably fastest way to stop unwanted input. The casts allowed are:

    • (int), (integer) – cast to integer
    • (bool), (boolean) – cast to boolean
    • (float), (double), (real) – cast to float
    • (string) – cast to string
    • (array) – cast to array
    • (object) – cast to object
    • (unset) – cast to NULL (PHP 5)
  2. PHP Filters

    PHP has a function filter_var (http://www.php.net/manual/en/function.filter-var.php) will helps you to compare a data type

    var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
    var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
    
  3. PHP inbuild() functions

    They are many functions in php, which can help you check the data types

    • is_array(): checks array
    • is_integer(): Check integer
    • is_object(): Check objects
    • is_float(): Checks Floating point number
    • ctype_digit(): Checks Integer

    and many more

  4. Regex Validation

    You will be able to find many resources on this on the internet.


Other Resources


Update

Your above method might turn into something like this

public function Article($title, $content) {
    if(is_string($title)) { $this -> title = $title; }
    if(is_string($content)) { $this -> content = $content; }
}
Read more

Array Not Returning Entire Element

Question by Bill

I think I simply have invalid syntax, but for the life of me I can’t figure it out. I have a nested array with 3 elements.

$screenshot = array( array( Carousel => "Library.png", Caption => "Long caption for the library goes here", ListItem => "The Library"));

I’m using a for loop to compose some HTML that includes the elements.

<?php
        for ( $row = 0; $row < 4; $row++)
        {
            echo "<div class="feature-title"><a href=" . $carousel_dir . $screenshot[$row]["Carousel"] . " title=" . $screenshot[$row]["Caption"] . " rel="lightbox[list]">" . $screenshot[$row]["ListItem"] . "</a></div>";
        }           
        ?>

My problem is that the “title” portion of the “a” tag only includes the first word, Long. So in the above case it would be:

<div class="feature-title"><a href="/images_carousel/1A-Library.png" title="Long" caption for the library goes here rel="lightbox[list]" class="cboxElement">The Library</a></div>

Can anyone shed some light on my error? Thanks in advance.

Answer by Ignacio Vazquez-Abrams

You forgot double quotes around the attribute values, so only the first word counts. The rest become (invalid) attribute names.

Answer by Starx

Make a habit of wrapping, the array index within double-quotes i.e. " for string indexes.

$screenshot = array( 
    array( 
        "Carousel" => "Library.png", 
        "Caption" => "Long caption for the library goes here", 
        "ListItem" => "The Library")
    );
Read more

How to count files in https?

Question by Jan-Dean Fajardo

Is it possible to count files under a directory when you’re only reading to https url? or only possible through ftp?

Answer by Starx

Only through ftp

http & https are protocols to view web applications. Features like directory listing are done from server, not through such protocols.

Explanation in case of php & apache server

When you are using commands like scandir() to read file and directory, its the server that does the reading for you. not any http or https link. The page you browse through such protocols will only deliver the output markup on the page.

Through these protocols, all files except server-side files can be delivered on their actual format.

Read more
February 29, 2012

Can't include file on remote server

Question by user918712

My problem is that I can’t include a file on a remote server.

<?php
  echo "Includingn";
  require_once("http://xx.xxx.xxx.xx:8080/path/to/myfile.inc");
  echo "Done..n";
?>

The script fails at the require_once function.
I’m running the script with: php -d allow_url_include=On script.php
but to make sure I have set allow_url_include and allow_url_fopen to On in php.ini

If I copy http://xx.xxx.xxx.xx:8080/path/to/myfile.inc to the browser I’m served the file.
I have also tried to include other remote files (on standard port 80), but still no luck

Where I get really confused is that everything works from my local computers at my office (mac, ubuntu), but not from our servers. I have tested it on 2 different servers, a virtual and a dedicated.
I can get the file with fopen().

Answer by Starx

This can be done by setting allow_url_include to on on php.ini.

But, as mentioned in comments, this opens a

huge

security hole on your application.

Read more

Get HTML source code as a string

Question by user1196522

I want the source code of an HTML page (1.html) to be used in another page (2.html). Further, I want to perform operations on it in 2.html.

Is there a way to do this?

EDIT: 1.html is a seperate public webpage and I do not have access to make changes to its source code. I have to do whatever I need only by using 2.html.

Answer by Starx

Its very simple

On 2.html use this jQuery snippet

$.get("1.html", function(response) { 
    alert(response) 
    //do you operations
});
Read more

htaccess for own php mvc framework

Question by paganotti

Good Morning,
I’m building my own php framework. I would want that all request pass to index.php file except request to js css jpeg, jpg, png file. So I want that a module independently to others has own assets folder with css images and js file.

The Structure is something similar:

application
   module1
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php

   module2
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php
  core
    here is core file framework

  index.php

How Can I do it?

Update

My modules are under application folder. assets folder must be public.
I want a simple small url like www.example.com/module1/images/test.jpg

instead long

www.example.com/application/module1/templates/assets/images/test.jpg

Answer by Starx

Actually, the default .htaccess of zf works well for this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Read more

How to use images for check boxes using CSS or may be Javascript?

Question by Imran

It is obviously very simple question but I have never done such thing before. My designer want some thing like this (it is on left side. Checked ones are grey and other white) for check boxes. Any help regarding this will be appreciated.

Answer by KooiInc

One idea is to use a span with background-image (empty box), hide a related checkbox, and assign a clickhandler to the the span (onclick: change checked attribute of the related hidden checkbox, change background image of the span accordingly).

Here is an example of that

Read more
...

Please fill the form - I will response as fast as I can!