Create Packages and laravelpackages in Laravel step by step.
First, let's take a look at Laravel official documentation, this is the latest 4.2 Documentation, if you want to see Chinese words click here, basically the same, this github library setup-laravel4-package, is also a step-by-step introduction to how to create a package, and related resources.
Here we will create an account package as an example:
1. If you use the php artisan workbench command for the first time, you need to configure the name and email in app/config/workbench. php and use the information when generating the package.
2. Use the following command in the project root directory to generate the initial package directory structure. Among them, -- resources is used to generate related resource directories such as view, config, and migrations.
1
php artisan workbench fstos/account --resources
The root directory of the package is in the workbench/fstos/account directory. Its structure is roughly as follows:
12345678910
/src /Fstos /Account AccountServiceProvider.php /config /lang /migrations /views/tests/public
3. Add the 'fstos \ Account \ accountserviceprovider' of the package to the "providers" array in app/config/app. php, so that the package will be loaded into the application.
4. Add an independent route file for this package. You only need to add the following code to the boot function of AccountServiceProvider and create the routes. php file in the root directory of the package.
12345
public function boot(){ $this->package('fstos/account'); include __DIR__.'/../../routes.php';}
The routing file is as follows:
123
Route::get('account/register', function(){ return "account register router";});
5. OK. Now you can test it in your browser. "account register router" is displayed. Next we will try to use controller
1
http://localhost/laravel/friends/public/account/register
6. Modify routes as follows:
1234
Route::get('account/register', array('as' => 'account.register.get', 'uses' => 'Fstos\Account\RegistrationController@getRegister') );
7. Create the RegistrationController. php file in controllers. The Code is as follows:
12345678910111213141516171819202122232425262728293031
<? Php namespace Fstos \ Account; use Illuminate \ Routing \ Controller as Controller; use Illuminate \ Support \ Facades \ View as View; use Illuminate \ Support \ Facades \ Config as Config; class RegistrationController extends Controller {/*** Display the registration form. ** @ return \ Illuminate \ Http \ Response */public function getRegister () {return "yes, use controller with route in packages! ";}/*** Temporarily unavailable */protected function setupLayout () {if (! Is_null ($ this-> layout) {$ this-> layout = View: make ($ this-> layout );}}}
Try to refresh the page. Aha, is it an error? "Fstos \ Account \ RegistrationController" cannot be found. Laravel does not automatically search for any directories by default. You need to specify the directory where the files are stored by the developer, however, it also provides a suggested path. For example, if the src/controllers directory is generated when artisan workbench is used, OK. Just add the automatic loading path.
8. composer in the package root directory. add "src/controllers" to the classmap field under autoload in json, and run php artisan dump-autoload and okok in the project root directory. Refresh the page, haha.
9. Next we will use the views file! Well, according to our understanding, we only need to modify the code in the controller, as shown below:
12345
public function getRegister() { return View::make('account.register'); // return "yes,use controller with route in packages!"; }
Apparently, he accessed the app/views/account/register. blade. php view template. We created this file and wrote the following content:
123
<div> we are shown in views!</div>
Refresh the page! Hahaha, fortunately, this rule ~, However, if we create a package, we don't want to place the view in the package in the app/views directory. It's a drop. We must integrate it into the package. Remember that there is also a view under src, we put the file in the src/views/account/register package. blade. in php, the content is as follows:
123
<div> we are shown in views under workbench!</div>
Refresh the page. I will clean the page. Is Laravel preferentially loading the template file under app/views? Delete app/views/account/and refresh the page again, again disappointed, the not found page appeared again. In Laravel, view files in the package are not automatically indexed, so we still need to do a little work!
10. When loading a view, specify it to search for the view file in the package. modify the code in the controller as follows:
12345
public function getRegister() { return View::make('account::account.register'); // return "yes,use controller with route in packages!"; }
Refresh the page again. The expected page appears. We can see that the account command space is used in the controller (For details, refer to the laravel official documentation ), so where is this space specified?
11. Let's take a look at AccountServiceProvider. $ this-> package ('fstos/account') in the boot function in the PHP file. this line of code actually specifies the Command Space of this package and the directory it is bound, this function also has the second (space name) and third (directory) parameters. However, both parameters have default values. The default value of namespace is account, the default directory is the src directory under the root directory, which is equivalent to the following statement:
1
$this->package('fstos/account',"account",__DIR__.'/../../');
OK, you can try ~
In the episode, I wrote it twice from the first record. It took a long time to log on to the system during the first record, but the record was lost. I tried it all, and I was so embarrassed, think about how important data backup is ~
This is my first release location: http://www.fstos.com/index.php/2014-05-26-09-01-36/81-laravel-packages
Laravel fails to create the first route. How can I handle the 404 error?
Route: get ('/users' one more '/'.
In The simulation of life 3C: \ Program Files \ Electronic Arts \ The Sims 3, A packages is also created in The MOD
Is it the latest version of resource. cfg?