Tagged: development RSS Toggle Comment Threads | Keyboard Shortcuts

  • softwareandme 6:42 am on October 1, 2009 Permalink
    Tags: cleanurl, development, , smarty   

    Smarty Cleanurl plugin 

    I developed a cleanurl which makes it easy to switch between classic urls and clean urls like ( http:/www.site.com/instrument/view/1 for http://www.site.com/?component=instrument&action=view&id=1 )

    before adding the component you should make some changes like in .htaccess and Smarty.class.php

    in Smarty.class.php add this line ( by default it is false )

    Code:
    /**

    • When set, smarty cleanurls are enabled
    • @var boolean

    */
    var $cleanurl = false;

    among the class attributes, i use it integrated with smarty and when i want to enable cleanurls i just tell to smarty to set this value true

    Code:
    $smarty = new Smarty();
    //
    //smarty initialization by you like template_dir etc
    //
    $smarty->cleanurl = true;

    you can use another method to enable cleanurls but for now it works like this.

    then you should change your .htaccess according to your urls, for example we should write a .htaccess for above url example

    Code:
    # this is the initialization
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /alem/alem/
    # these are the rewrite conditions
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # and finally, the rewrite rules
    RewriteRule ^([a-zA-Z0-9\-]+)/?$ index.php?component=$1 [L,QSA]
    RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ index.php?component=$1&action=$2 [L,QSA]
    #id is is an integer
    RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([0-9\-]+)/?$ index.php?component=$1&action=$2&id=$3 [L,QSA]

    the last line of this .htaccess sample makes your urls clean. You should change the .htaccess according to your url structure.

    And the plugin itself should be added under the plugins directory.

    Then all you have to do is just putting your urls as normal urls in cleanurl’s url attribute like this one

    Code:
    {cleanurl url="?component=instrument&action=instrument&id=1" name="I am a Link"}

    it will give this output when cleanurls are not enabled

    Code:
    I am a Link

    and this one when cleanurls are enabled

    Code:
    I am a Link

    url and name attributes are required and there are other attributes like;

    title : when title attribute is set it adds a title to your link e.g.<a title="TITLE", if you dont set the title name attribute will be shown as the title

    extension: if you add and extension to your clean urls like instrument/view/1.html you should set this value as {cleanurl extension=”.html” ….. }

    image: if you want to give link to an image you should set the image attribute and it will have a link and with image_class attribute you can give a class for your image

    When using with lists you should use active and id lets explain it with an example if you have a list like this, before this

    $active should be assigned with smarty assign or you can just use $smarty.get.action for active attribute, for this example in php you should do this

    Code:
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    $smarty->assign('action',$action);
    
    Code:
    • {cleanurl url=""?component=instrument&action=view&id=1" name="Link 1" id="view" active="$action"}
    • {cleanurl url=""?component=instrument&action=review&id=1" name="Link 2" id="review" active="$action"}
    • {cleanurl url=""?component=instrument&action=order&id=1" name="Link 3" id="order" active="$action"}
    • {cleanurl url=""?component=instrument&action=list" name="Link 4" id="list" active="$action"}

    when the assigned value for id and active matches, the cleanurl will not show a link it will show only the text inside a span

    if you want to use this for a horizontal list also you can try this css

    Code:
    .horizontal_list { list-style-type: none; padding: 0; margin: 0; }
    .horizontal_list li { display: inline; }
    .horizontal_list li a { padding-left: 7px; }
    .horizontal_list li span { font-weight: bold; color: #3B5998; padding-left: 7px; }
    

    and here is the code, and one thing to add i always use base href in my sites if you dont use it you may want to change the plugin and add the base_url in front of the cleanurl, that s up to you.

    I hope it helps,

    just copy the below code and save it and copy the saved file under smarty plugins fodler, function.cleanurl.php

    Code:
    <?php
    /**
    * Smarty plugin
    * @package Smarty
    * @subpackage plugins
    */
    </code><code>
    /**
    * Smarty {cleanurl} function plugin
    *
    * Type:     function<br>
    * Name:     cleanurl<br>
    * Date:     July 1, 2009<br>
    * Purpose:  Cleans the urls to make them search-engine-friendly<br>
    * Input:<br>
    *         - url    = url for cleaning
    *          - name   = name for link
    *          - title   = title for link
    *
    * Examples: {cleanurl url="http://cleanurl.com/?sec=software&com=firma&act=info&id=1" name="Clean"}
    * Output:   http://cleanurl.com/software/firma/info/1
    * @link http://smarty.php.net/manual/en/language.function.cleanurl.php {cleanurl}
    *      (Smarty online manual)
    * @author   Onur GUZEL onur.oguzel[at]gmail.com
    * @version  0.1
    * @param array
    * @param Smarty
    * @return string
    */
    function smarty_function_cleanurl($params, &$smarty) {
    foreach ( $params as $_key => $_val ) {
    switch ($_key) {
    case 'url':
    case 'name':
    case 'title':
    case 'extension':
    case 'image':
    case 'image_class':
    case 'active':
    case 'id':
    case 'askmessage':
    $$_key = (string)$_val ;
    break;
    default :
    $smarty->trigger_error ( "cleanurl:  no permission to add extra attribute '$_key'", E_USER_NOTICE );
    break;
    }
    }
    
    if (empty ( $url )) {
    $smarty->trigger_error ( "cleanurl: missing 'url' parameter", E_USER_NOTICE );
    return;
    }
    if (empty ( $name )) {
    $smarty->trigger_error ( "cleanurl: missing 'name' parameter", E_USER_NOTICE );
    return;
    }
    
    if( empty($title) )
    {
    $title = $name;
    }
    
    $urls = explode('&amp;',$url);
    $urlstring = '';
    if( sizeof($urls) )
    {
    $j=0;
    foreach( $urls as $url_ )
    {
    $url_tmp = explode('=',$url_);
    $urlstring .= $url_tmp[1];
    if($j!=sizeof($urls)-1) $urlstring .= '/';
    $j++;
    }
    }
    else
    {
    $smarty->trigger_error ( "cleanurl: please check the url attribute", E_USER_NOTICE );
    }
    
    if( isset($extension) )
    {
    $urlstring .= $extension;
    }
    $stringurl = '';
    
    $image_src = '';
    if( count($image) )
    {
    $image_src = '<img src="'.$image.'" border="0" alt="'.$name.'" title="'.$title.'"/>';
    $name = $image_src;
    }
    
    if( $smarty->cleanurl )
    {
    $stringurl = '<a href="'.$urlstring.'" title="'.$title.'">'.$name.'</a>';
    }
    else
    {
    $stringurl = '<a href="'.$url.'" title="'.$title.'">'.$name.'</a>' ;
    }
    
    if( isset($active) && isset($id) && $active == $id ) $stringurl = '<span title="'.$title.'">'.$name.'</span>';
    
    return $stringurl;
    }
    
    ?>

    Regards

     
  • softwareandme 5:19 am on October 1, 2009 Permalink
    Tags: development, introduction, ,   

    php, How I met and decided to use again 

    First i heard about php in a computer magazine, when i was in high school. After that I met apache and their good friend mysql came to meeting.  Then in the university years, the good friend Slackware joined us. It was fun to code with php and python while trying to learn c. They were easy and clean languages,  php syntax was so similar to c and then i decided to use php on the web projects, c was not something i was looking for i met c++ then oop adventure started. I had some ideals and tried to develop a project for my license thesis, I used C++ in the project. It was an unsuccessfull project, so no need to mention it.

    I was a C++ programmer and learned and studied Object Oriented Programming concepts a lot more than the procedural concepts. I just used procedural concept while learining how to code in c then i used c++ and tried to learn it. I am not a C++ professional but i first met with C++ seven years ago, i developed some projects with c++ but i had to develop with java and c#. After learning c++, java was not so hard, and i have found an equatition for c#

    c# = (c+ – +) + (java)

    c# was a really easy and good language for a c++ and java programmer.

     I was coding with php for years, since high school days. Php was not an object oriented language I was not able to use classes ( i mean php 3). I was writing some basic codes with php. Some day i have seen that object oriented principles applied in php. When i was developing code with c#, php’s oop was getting more stable. I was a web developer who can code with c# , php and java using netbeans, visual studio and vim. I didnot develop big projects with netbeans or visual studio, I was developing some other coders’, who left the work, libraries.

    Java and C# did not have clean code for me and developer was not free. Performance issues were making me so mad. Netbeans/Java or Eclipse/Java is more free than VS/C#. I returned to old friend php and realized that developers really did a good work and add the oop in php in a good manner. Php has still some problems like memory management but who doesn’t ?( c# or java :)   )

    Php and c integration always makes you feel good, you know c stands under the hood and you can add more cylinders and valves to the engine if you want to do that. Developers can develop php modules of their own if they need even they can change or add their rules to the language.

    Community support is really better than the other popular programming languages and python community is a good community like php community. There are a lot of magazines, books, professioanls, sites, irc channels, conferences, companies  about php.

    After php 5 and oop, php started to run ( it was walking )  and then we have seen some scalable huge projects writtien in php. sunn’s and ms’s stories about scalability were not right about their development environments and frameworks. Good software engineers told and show ( sunn and ms and the other big lier companies ) them developing scalable and good program is not something related to the language you use, it is something related to your software design and engineering. Design comes with freedom, if someone or something is not free, design wont be there; obligations will take place instead of design and freedom. Linux also freed the php,apache and mysql and gave a good development environment to free developers. I am a really advocate for free software ( free is not related to money, it is the free in freedom ) and enjoy when i see a succesfull free software project.

    After I have finished military service and as a coincidence i found jobs related to php and meet with the oldfriend again, now as a professional i have been developing oop php for three years.

    Blogged with the Flock Browser
     
  • softwareandme 4:06 am on July 31, 2009 Permalink
    Tags: developer, development, open source, program, programmer, , software engineer   

    Greetings,

    I am a software engineer / programmer who developed programs with java,c++,c# and php with little bash and python, but mostly php and c++. I call my self  “software repairer” because i always had to develop somebody else’s projects and repair them, i am not saying it is a bad. I have also projects more than ten, none of my projects have been finished, they are all ( commercial ones for the companies i work and the free ones have the same end ) un-finished. They are all in beta phases or in 0.0.1.1234 version.

    If you ask me why, i can give a lot of excuses and also detailed information about the excuses but who cares, i have a lot of unfinished work for 4 years. They are all waiting to see the sun light.

    I am an open source and free software enthusiast, i learned a lot from the open source projects, they gave me the chance to know how to develop a software like a real professional. It is important to know how not to write code also, my commercial experiences in some companies taught me how not to write code. I try to obey the golden rules of software engineering, try to follow IEEE standards in all of the life cycle and development phases.

    I am using Slackware and Debian, nowadays i have to use Windows a lot.

    I hope i can write my experiences about software and software development, I also have to say that there are a lot of things i have to learn about software engineering, but sharing knowledge makes it precious. If you dont share your knowledge it is just useful to you, it means that it is not useful in real if you are so selfish.

    I also play bass guitar, i am an amateur bass guitarist. Sometimes i may write something related to music espacially bass guitars, i am not a musician i cant write anything music theory.

    I will try to add my daily experiences as a developer,  they will be mostly about web programming with php, postgresql, apache, mysql, c++, wt.

    Hope you will like my posts.

    Thanks for reading this article.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Follow

Get every new post delivered to your Inbox.