Building a background folder in the phpLaravel framework today I have studied how to add backend folders to controllers in the Laravel framework. I have found some small patterns. let's share them with you.
In general, we add our controller directly in the controllers folder and then routes. in the php route table, find the corresponding controller and action for processing. but now there is a problem. if our front-end and back-end are in the same project, the front-end and back-end controllers will be in the same project. of course there is no way to differentiate. we can add Front and Admin tags to Front-end controllers and back-end controllers respectively.
Here I provide you with a way to separate the front and back-end controllers.
First, create an admin folder in the controllers folder.
Method 1: directly create a controller in the background. for example, we can normally write our contents in AdminController. php.
In the routing table,
Route: get ('admin', 'admincontroller @ index ');
We found that the program reported an error, saying that the controller AdminController could not be found.
Open the command line tool and go to the directory where the project is located, that is, artisan and composer. json,
Run the command composer dumpautoload or composer dump-autoload.
This is to optimize automatic loading.
After running the command, go to the composer directory in the vendor directory,
Let's take a look at our autoload_classmap.php file and find that there is an additional AdminController record.
By the way, refresh the browser to see the content of the background controller.
However, this disadvantage is that you must run this command every time you add a new controller ..
Note that if your background folder contains the same controller as the foreground, the program reports an error.
The system prompts that the backend and the front-end have a controller name conflict.
The solution here is to use the namespace in the background for processing.
For example, our backend also has an IndexController controller.
Namespace Controllers \ Admin;
Class IndexController extends \ BaseController {
Public function index (){
}
}
?>
Note that after namespace is used, all static classes used in the namespace are the classes of the current namespace by default. so sometimes we need to use Use View; use... and so on.
In the routing table, we also need to use Controllers such as Controllers \ Admin \ IndexController @ index to process requests.
Well, this is what I learned and I will share it with you.
Persistence makes life valuable.
Best Wishes.