The Hamiltonian for the harmonic oscillator (with $\hbar = m = 1$) is given by: $$\hat{H} = -\frac{1}{2}\frac{d^{2}}{dx^{2}} + \frac{1}{2}\omega^{2}x^{2}$$ This is assumed to be an operator on $\mathscr{S}(\mathbb{R}^{d})\subset L^{2}(\mathbb{R}^{d})$. One usual approach to solve it is the following. Define the operators $a, a^{\dagger}$: \begin{eqnarray} a = \sqrt{\frac{\omega}{2}}\bigg{(}-\frac{d}{dx} +x^{2}\bigg{)} \quad \mbox{and} \quad a^{\dagger} = \sqrt{\frac{\omega}{2}}\bigg{(}x-\frac{d}{dx}\bigg{)} \tag{1}\label{1} \end{eqnarray} so that the Hamiltonian becomes: \begin{eqnarray} \hat{H} = aa^{\dagger}-\frac{1}{2}I = a^{\dagger}a + \frac{1}{2} I \tag{2}\label{2} \end{eqnarray} and $a,a^{\dagger}$ satisfies $[a,(a^{\dagger})^{k}] = k(a^{\dagger})^{k-1}$. The eigenstates of $\hat{H}$ are given by Hermite polynomials: \begin{eqnarray} \phi_{0}(x) := \pi^{-\frac{1}{4}}e^{-\frac{1}{2}x^{2}} \quad \mbox{and} \quad \phi_{k}(x) := \frac{1}{\sqrt{k!}}(a^{\dagger})^{k}\phi_{0}(x) \tag{3}\label{3} \end{eqnarray} Now, as far as I understand, when we move to Dirac's approach, the state $|k\rangle$ is actually an expansion in terms of the position eigenvectors, that is: \begin{eqnarray} |k\rangle = \int dx \phi_{k}(x)|x\rangle \tag{4}\label{4} \end{eqnarray} However, one can attack the problem directly using Dirac's formalism.
Question: Using Dirac's approach directly, the operators $a$ and $a^{\dagger}$ now act on $|k\rangle$ instead of its components $\phi_{k}(x)$, so that $a$ and $a^{\dagger}$ must act on $|x\rangle$. However, the expression of the Hamiltonian $\hat{H}$ is precisely the same as in (\ref{2}). What is the connection between the two approaches? How are both $a$ and $a^{\dagger}$ (in both formalisms) related? I'm a bit confused.