ThinkPHP cross-module call and method not found

Source: Internet
Author: User

During php development, sometimes I write a method in a module, but I want to call it in another module. In this case, cross-module calls are required, for example, I now have a usersAction module with a test () method. How can I call it in company? There are two ways

The code is as follows: Copy code

1: $ User = A ("User"); // instantiate the UserAction controller object

$ User-> importUser (); // call the importUser operation method of the User module

Here, A ("User") is A shortcut, which is equivalent to the following code:

The code is as follows: Copy code

Import ("@. Action. UserAction ");

$ User = new UserAction ();

In fact, there are more simple calling methods than method A in this example, for example:

The code is as follows: Copy code
R ("User", "importUser ");

// Call the importUser operation method of the UserAction controller remotely

The above is only called in the current project. If you need to call methods between multiple projects, you can do the same:

The code is as follows: Copy code

$ User = A ("User", "App2"); // instantiate the UserAction controller object of the App2 project

$ User-> importUser ();

// Remotely call the importUser operation method of the UserAction controller of App2 project

The code is as follows: Copy code

R ("User", "importUser", "App2 ");

My example:

A project is divided into two groups: admin and home.

Home is a group by default:

When instantiating the module (the current position is instantiated in the index method of the IndexAction class in admin)

The code is as follows: Copy code

Import ("@. Action. Home. UserAction ");

$ User = new UserAction ();

$ User-> show ();

$ User-> add ();

Note: The called method must be public.

Another simple method is to write the test method to common. php, because the file has been called by default during loading.


Solution not found


I don't know if this is a BUG. If the called Action has the same name as the current Action, ThinkPHP will report that the method cannot be found (maybe in the called Action ).
For example, this problem occurs when the Common/Config method is called in Admin/Config. Solution:
No way. Change the name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.