phplaravelmikrotik

How to Create New User in Mikrotik Using Laravel?


I am trying to add new user using Mikrotik PHP API in Laravel. I can use the API for other usage (like view user information) but when I try to create new user, it gives me nothing, even no error, just redirect me to intended page.

My route is

Route::post('/isp/addUser', 'MainController@addUser');

My Form request is

<form method="POST" action="{{ action('MainController@addUser') }}">
   {{ csrf_field() }}
   @foreach ($networks as $network)
        <input type="hidden" name="user_name" value="{{ $user->name }}">
        <input type="hidden" name="ip" value="{{ $network->ip }}">
        <input type="hidden" name="user" value="{{ $network->m_username }}">
        <input type="hidden" name="pass" value="{{ $network->m_password }}">
   @endforeach
        <button type="submit" class="btn btn-primary btn-sm text-uppercase">
                <strong>add</strong>
        </button>
 </form>

My Controller method is

public function addUser(Request $request) {
        $API = new routeros_api();
        $API->user_name = $request->user_name;
        $API->ip = $request->ip;
        $API->user = $request->user;
        $API->pass = $request->pass;

        if ($API->connect($request->ip, $request->user, $request->pass)){
          $API->comm("/ppp/secret/add/name={{ $API->user }}/password=123456/service=ppoe/profile=1-MBPS");
          $API->disconnect();
        }

        return redirect('/isp');
    }

This is not even giving any error, so I am not getting any clue. I think I am not sending the request properly, but got no clue how to do it.


Solution

  • for creating user you should send

    $API->comm('/user/add',['name'=>$username,'password'=>$password,'group'=>'read']);
    

    for more information you can check https://mikrotik.com/documentation/manual_2.4/System/Users.html

    hope this helps