Pogledajte određenu poruku
Staro 13. 11. 2006.   #7
Ilija Studen
Direktor Kombinata
Invented the damn thing
 
Avatar Ilija Studen
 
Datum učlanjenja: 07.06.2005
Poruke: 2.669
Hvala: 44
119 "Hvala" u 64 poruka
Ilija Studen će postati "faca" uskoroIlija Studen će postati "faca" uskoro
Default

PHP ima dosta gotovih funkcija i biblioteka, ali na to se više može gledati kao na standardnu biblioteku nego kao na nekakve prečice... Zbog svoje stroge sintakse količina kodu koju moraš da napišeš je daleko veća nego kod recimo Pythona i Rubyja (a posebno Rubyja).

Evo ga jedan relativno jednostavan kontroler pisan u Rubyju (iz realnog projekta) čisto da imaš jedan primer koda. Primeti koliko se stvari NE piše i šta sve nije obavezno. C-oliki jezici su krajnje nezgrapni u poređenju sa ovim.

Kôd:
class NoteController < ApplicationController

  model :user
  before_filter :login_required

  layout "standard"

  def index
    @all_notes = @user.notes
    @page_title = "TRACKS::All notes"
  end

  def show
    @note = check_user_return_note
    @page_title = "TRACKS::Note " + @note.id.to_s
  end

  # Add a new note to this project
  #
  def add
    note = @user.notes.build
    note.attributes = @params["new_note"]

    if note.save
      render_partial 'notes_summary', note
    else
      render_text ""
    end
  end

  def delete
    note = check_user_return_note
    if note.destroy
      render_text ""
    else
      flash["warning"] = "Couldn't delete note \"#{note.id.to_s}\""
      render_text ""
    end
  end

  def update
    note = check_user_return_note
    note.attributes = @params["note"]
      if note.save
        render_partial 'notes', note
      else
        flash["warning"] = "Couldn't update note \"#{note.id.to_s}\""
        render_text ""
      end
  end

  protected

    def check_user_return_note
      note = Note.find_by_id( @params['id'] )
      if @user == note.user
        return note
      else
        render_text ""
      end
    end
end

Poslednja izmena od Ilija Studen : 13. 11. 2006. u 20:31.
Ilija Studen je offline   Odgovorite uz citat