简体   繁体   中英

How to call other function from the same controller in Laravel application?

I'm very new to Laravel and I was given a Laravel project, where I need to add some new features. The person, who has previously worked on that project hadn't left even a single comment in the code and now I must make my own scenarios about the features. I have a controller, defined with some functions (dashboard, show_project, save_project etc.) and in one of my function, I need to use the result of calling other function.

In the concrete example, the call is made from " http://127.0.0.1:8000/username/project_slug " - there is a button "Save" and post function, called on onClick event.The function, whose output I need is normally called on " http://127.0.0.1:8000/username/project_slug/svg ", which returns a view.

For better understanding, there's an example of the flow:
The user wants to save his/her project (an UML diagram) but in order to have a thumbnail, a function which generates a view (SVG format) will be called and the idea is, to take the HTML content of the page, which is on " http://127.0.0.1:8000/username/project_slug/svg " and to pass it to another API in order an image to be generated.

So far, I tried with cURL , file_get_contents , file_get_html , render methods but when I return the output, the server just keeps waiting and shows no error messages.

//The both functions are in ProjectController.php

/**
 * A function, for saving the json file, where the whole of the diagram 
 * components are described. From the frontend we receive the project_id and  
 * the project_data(the json content).
 */
public function save_project(Request $request) {
        $input = $request->only(['project_id', 'project_data']);

        /*
          Here we need to call the other function, to render the HTML content
          and to pass it to the other API. Then we save the result with the
          other information.
        */

        /*
          What I've tried?
          $new_link = 'http://' . $_SERVER['HTTP_HOST'] . "/$username" 
                                              ."/$project_slug" . "/svg";
          $contents = file_get_contents($new_link);
          return $contents;
        */

        //In the same way with cURL.

        $project = Project::where('user_id',session('userid'))
                          ->where('id',$input['project_id'])->first();
        $project->project_data = json_encode($input['project_data']);
        if($project->save()) {
            return ["status"=>"saved"];
        }
        else {
            return ["status"=>"error"];
        }
    }

/**
 * A function, which takes the the Json content (project_data) from the 
 * database and passes it to the view, where the Json is transformed in HTML 
 * tags. 
 */
public function generate_svg(Request $request,$username,$project_slug) {
        if(session('username')!=$username) {
            return redirect("/");
        }

        $userid = session('userid');

        $project = Project::where([
            'user_id' => $userid,
            'slug' => $project_slug,
        ])->first();

        if(!is_null($project)) {
            return view('svg',compact('project'));        
        }
    }

I've read about some possible ways, including Guzzle request but maybe I haven't understood correctly the idea:

If I need to make a Guzzle request from my controller to the other function inside my controller, do I need an API configuration?

What I mean? Example: Before saving the project, the user is on this URL address " http://127.0.0.1:8000/hristo/16test ". Inside the controller, I have in session variables the token, the username(hristo) and i can get the project_name(16test) from the URL but after passing this URL to the generate_svg function, there is no indication of error or success.

So I'm missing some kind of token information?

If you just need the response of the other function you can just use

$response = $this->generate_svg($request, $username, $project_slug);

If you'll need to use this function from a different controller you can use this

app('App\Http\Controllers\UsernameController')->generate_svg($request, $username, $project_slug);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM