Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • EsqJ 11 posts 54 karma points
    Jan 25, 2015 @ 13:17
    EsqJ
    0

    Newbie Question From a PHP developer - 'custom' code?

    I am a PHP developer but looking to start an Umbraco project, i understand the basics of creating a simple content managed site using doc types etc.

    What i want to understand though is how i implement custom code that is not text/image content? for example i have an API i want to connect to, to bring back certain data and display that data on one of my pages or i have a form that i want to capture and then send the details to an API?

    How do i get that 'code' into my page, what language do i write it in and where? i know i need to do it in .net (razor?) but my understanding of .net is limited, all i know is it involves dll files and other crazy stuff. I presume i can't just write a single block of code in one place to achieve something like i could in PHP?

    Am i over thinking how tricky this could be?

    If someone could explain this to me like i am 5 years old that would much appreciated :D

     

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 25, 2015 @ 21:25
    Andy Butland
    1

    Hi EsqJ

    My daughter is just coming on 5 so tried this out with her :-).  She wasn't very interested I'm afraid, but hopefully I can still help you a bit!

    I'd suggest actually the questions you are asking don't relate so much to Umbraco, more to .Net itself, and particularly in building web application using ASP.Net MVC.  So it's probably worth a bit of background reading on that.

    But as to the specific question of where to put your code, well you actually have a choice.  The "dlls and other crazy stuff" are generated from your compiled code, and most .Net developer will look to write most of their code this way.  So this means using Visual Studio, writing C# classes etc.  That way you get compile time checking, "intellisense" in your IDE... all those types of crutches .Net developers need and get mocked for!

    You don't have to do that though - with MVC, your views are written in Razor, which is basically a hybrid of C# and HTML.  Not disimilar to how you would write PHP code, but it's a bit different.  Rather than a strict delimiter likeetc. you use the @ symbol to switch to C# and a standard HTML tags to go back to HTML.  99% of the time it works as you'd expect - occasionally you have to work around some things where the compiler can't work out which you are writing.

    Because of this though you can write a lot of C# code, including connecting to external APIs etc. in your view.

    I say "can", as most MVC developers - in any language actually, not specific to .Net at all - wouldn't look to do this.  They would rather put this type of thing in a controller or model class.  

    Finally coming back to Umbraco, you can certainly write this type of thing in your views.  It's not my preferred method as I've blogged previously - I'd rather go with route hijacking and put logic like that in my controller - but there's nothing to stop you if that works for you.  And to get started, coming from a more dynamic language like PHP, you might find it does.  I'd suggest though if you start to find your views getting fairly complex, and particularly repeating yourself a fair bit, it's work looking into the stricter MVC ways of doing things in Umbraco, using route hijacking as mentioned, surface controllers etc.

    Hope that helps get your started anyway.

    Cheers

    Andy

     

  • EsqJ 11 posts 54 karma points
    Jan 25, 2015 @ 21:37
    EsqJ
    0

    Hi Andy,

    Thanks for the reply! i have had a look at using Razor syntax in my templates for simple things like looping and basic logic and in that sense its a lot like PHP.

    So in theory using Razor i could do things such as connect to an API and other more complicated stuff right in the view? or i guess in a macro?

    I did have a look at surface controllers in this video....

    http://umbraco.tv/videos/developer/fundamentals/surface-controllers/introduction/

    In the video though he seems to 'create' an umbraco installtion in his .net project, but ovbiously my umraco installation already exists, can i do what he is doing here even if my umbraco installation exists?

    If i did just do all the coding in my template, is there anything 'wrong' with this? are their limitations? it seems the most simple way to me but obviously most people don't do it for some reason?

    Also, in terms of say collecting data from a form and doing something with it, in PHP i would have a page that collects the data and the next page sends the data off somewhere, im not sure this method would quite work by just writing all my code in my view?

     

     

     

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 25, 2015 @ 22:04
    Andy Butland
    0

    I'd say it's like a lot of things in software development, it's not really black and white - you certainly can connect to your API from the Razor code in your view, but the best practice way of doing things would be too keep logic in the view to the basic things like loops and conditionals. I think you just have to take a call for your purposes - could be that if it's a fairly simple site and this is the only deviation from these "best practices", you might think it's OK.  But if things get more complex you'd probably want to structure your code a bit more.  

    For form handling though, you do need something the post back to as you say - and within Umbraco that's what surface controllers are for.  You can find pretty good documenation for that here.

    Andy

  • EsqJ 11 posts 54 karma points
    Jan 26, 2015 @ 00:26
    EsqJ
    0

    Still trying to get my head around it, its a big jump from using a language where i get by with just notepad and not much code to something where im using visual studio, creating a dll and tonnes of other stuff just to capture a form post!

    Almost tempted to just post my form to .php pages on the same server haha *hides*

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 26, 2015 @ 10:24
    Andy Butland
    0

    One thing I forgot to mention, there is this magic App_Code folder you might want to check out - any C# files you put in there will get compiled on the fly, so you can create your surface controller for handling your form post and put it in there.  And thus avoid VS.Net and stick with just the Umbraco UI and Notepad.  So it is possible to build sites that way if it works better for you.

    Hopefully you stick around for a bit before defecting back to Wordpress anyway!

    Andy

  • EsqJ 11 posts 54 karma points
    Jan 26, 2015 @ 17:43
    EsqJ
    0

    Are there any tutorial videos that go through step by step setting up a form/capture the post and that implement that into umbraco? as i mentioned earlier, the video i linked to, they seem to have to 'include' umbraco in their project, which confused me. I already have umbraco installed, i just want to add this functionailty into it if that makes sense?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 26, 2015 @ 21:17
    Andy Butland
    0

    Have a look at http://umbraco.tv/ specifically this one.  You do have to register for a subscription, but it's well worth it if you are starting out to get up to speed.

    Andy

Please Sign in or register to post replies

Write your reply to:

Draft