This paper proposes a near‐optimal controller design for the constrained nonlinear affine systems based on a Recurrent Neural Network (RNN) and Extended State Observers (ESOs). For this purpose, after defining a finite‐horizon integral‐type performance index, the prediction over the horizon is performed using the Taylor expansion that converts the primary problem into a finite‐dimensional optimization. In comparison with other controllers of the similar structure, the proposed method is capable of dealing with output constraints by employing the Control Barrier Function (CBF). The class of the output and input constraints are of the box‐type. Moreover, whereas several safe control approaches are proposed regardless of the performance of the closed‐loop system, this paper aims at achieving a near‐optimal performance as far as the constraints permit. As a result, a constrained optimization problem is achieved, where the online solution is obtained using a rapidly convergent RNN. Stability and the ease of implementation are some of the advantages of this network making the algorithm more reliable. Moreover, integrated stability analysis of the closed‐loop system that includes the dynamic RNN reveals that the closed‐loop system is stable in the sense of the Lyapunov stability theory. The effectiveness of the proposed control method in terms of the tracking performance and constraint satisfaction is illustrated through a simulating example of two‐inverted pendulums system. The results indicated advantages of the proposed method as compared with the recently published methods in well‐known literature.