8 Replies Latest reply on Jun 8, 2011 6:17 AM by pauland

    How to prevent hard-coding url's

    flexillu10 Level 1

      Hi,

       

      I have an adobe air application, that makes use of a few php scripts on my server.

       

      I use many httpservice's and they all have urls like:

       

      http://myserver.com/scripta.php

      http://myserver.com/scriptb.php

       

      Obviously there are many issues doing this.

       

      So how would i go about preventing this? Just store them in one file?


      Could someone give me a very simple example of how to do this?

       

      Any advice or links to info/tutorials is greatly appreciated.

       

      Thanks.

        • 1. Re: How to prevent hard-coding url's
          pauland Level 4

          I generally use a single php script and pass as a parameter the action I want to be performed. Then the script can do whatever it needs to do to perform that action. If new functionality/a new action is required I add it to the script. Essentially there's a switch statement looking at the action required then calling the appropriate routine to handle it. This one script can also do any security vetting making sure the user is logged in, etc.

          • 2. Re: How to prevent hard-coding url's
            flexillu10 Level 1

            I was thinking more inside the application code. So if http://myserver.com changes i won't have to change the url in 15 places?

             

            I've seen a mathod using an config.xml file with the server url in. So i'm looking for a simple example of this.

             

            Thanks for your info though as i am also currently re-orgainising my php scripts, wouldn't a massive switch statment be in efficient?

            • 3. Re: How to prevent hard-coding url's
              Nishant Jha28 Level 2

              Are you using some framework to do this?

               

              Neway the solution is to use a local XML that is loaded at the start of app. This xml should have the settings for all the urls used.

               

              regards

              Nishant

              • 4. Re: How to prevent hard-coding url's
                pauland Level 4

                flexillu10 wrote:

                wouldn't a massive switch statment be in efficient?

                 

                Most of the systems I write don't need a large number of actions to be performed and it's not been an issue.

                 

                For seperate URLS, as has been suggested, an XML parameter file could be used to allow customisation.

                • 5. Re: How to prevent hard-coding url's
                  flexillu10 Level 1

                  Most of the systems I write don't need a large number of actions to be performed and it's not been an issue.

                  Thanks, great.

                   

                  Does this mean my application is written badly? I started off with a small application and its gradually got bigger and bigger.

                   

                  Now i'm sat on numerous http service requests returning different types of data. Seems like every component uses a http service.

                   

                  I have a login script, a script that loads a tree component with xml data, a search script, a download script and a log out script.

                   

                  This screams bad practice to me, what am i missing? A framework like Nishant suggested?

                  • 6. Re: How to prevent hard-coding url's
                    pauland Level 4

                    flexillu10 wrote:

                     

                    Seems like every component uses a http service.

                     

                    I have a login script, a script that loads a tree component with xml data, a search script, a download script and a log out script.

                     

                    This screams bad practice to me, what am i missing?

                     

                    Sounds like you aren't using an MVC architecture. I usually have a model component that looks after the data used by the UI. UI components never request data remotely. They signal (via events) a controller to request that the user logs in. The controller will co-operate with the model, so that in effect the model aggregates remote data requests and updates the model data as data is retrieved. The UI is updated via binding and/or event notification.

                     

                    All my remote communication is controlled by one component taht packages up requests for the server and handles the response. Many people use more sophisticated and formalised MVC architectures such as PureMVC, cairngorm, .. etc.

                     

                    Five actions is nothing.

                     

                    Paul

                    1 person found this helpful
                    • 7. Re: How to prevent hard-coding url's
                      flexillu10 Level 1

                      brilliant thanks. I guess i should look towards using a MVC.


                      Am i correct in thinking that a php framework like zend amf would do a similar job?

                       

                      also, could you give a brief example of using an XML parameter file in an adobe air app?

                      • 8. Re: How to prevent hard-coding url's
                        pauland Level 4

                        I don't usually need a parameter file - it's just plain XML processing - there are a ton of tutorials out there.

                         

                        Zend amf is another step up. Practice makes perfect and all that..