Laws are a reflection of the morality of the times. Especially in a democracy.
So, morality is quite relevant.
Things that are illegal are considered immoral (sodomy, gambling, drug laws, etc). They change as morality changes.
To an extent.
For example; there is nothing really moral about a parking citation, or certain driving restrictions (whereas others like drunk driving would be morality in a sense) or even things like public decency laws regarding clothing. I'm not sure those are moral so much as they just keep society generally moving. Actually most laws can be reduced to money concerns. Most drug laws, specifically those about opiates, developed because of a fear that it would negatively impact the workforce which, at the time, was very dependent on manual labor. Whereas now zoning out in front of a computer screen is pretty easy to do high, or may even improve someone's demeanor in a service job. So, no, I don't think it has anything to do with morality.
Zoning laws definitely have nothing to do with morality.
I'd actually argue there is nothing moral about abortion, yet it is legal, and should be legal because there are very legitimate instances where it is necessary. The unintended consequence is people end up using it as a magic eraser for their irresponsible sex life.
Other things simply are not laws because we can't enforce them, or cannot enforce them in a way that would be "moral". Arguably there are good public health concerns regarding sodomy, but unless you're willing to create a secret police to go around and hide in people's bedrooms there is no way to regulate safe sex practices. Make sure gay and straight people wear condoms and such. It really has nothing to do with "two consenting adults". We outlaw all sorts of things "two consenting adults" could get together and do. So there, you have a moral concern over the right of individual privacy but also a bureaucratic concern of "why would we bother wasting money on something we can't enforce".
So really it is a framework of obedience and disobedience.
That's one of the reasons I *cringe* when I hear "law abiding gun owners" versus "criminals". Most criminals, excuse me, ALL criminals are "law abiding" until that day they decided not to. Like Fanboii pointed out, this is an obedience test. Because the other, rather two-faced side of the courtroom, is the law never says "don't do this" it in fact says "don't
get caught doing this". I still smoke pot in the privacy of my own home, and while the law totally frowns on this, they'd also frown on just about every measure they'd have to
catch me. That's the reason the law so often protects people like Ben Roethlisberger or Michael Jackson; because they can afford a lot of privacy, and it's much harder to conduct proper investigations against people who can pay off witnesses and have others sweep away evidence for them. So are laws are very much an obedience test.