在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):dahabit/Laravel-4-Generators开源软件地址(OpenSource Url):https://github.com/dahabit/Laravel-4-Generators开源编程语言(OpenSource Language):开源软件介绍(OpenSource Introduction):This Laravel 4 package provides a variety of generators to speed up your development process. These generators include:
Prefer a Video Walk-through?InstallationBegin by installing this package through Composer. Edit your project's
Next, update Composer from the Terminal:
Once this operation completes, the final step is to add the service provider. Open
That's it! You're all set to go. Run the
UsageThink of generators as an easy way to speed up your workflow. Rather than opening the models directory, creating a new file, saving it, and adding the class, you can simply run a single generate command. MigrationsLaravel 4 offers a migration generator, but it stops just short of creating the schema (or the fields for the table). Let's review a couple examples, using
If we don't specify the <?php
use Illuminate\Database\Migrations\Migration;
class CreatePostTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post', function($table)
{
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post');
}
} Notice that the generator is smart enough to detect that you're trying to create a table. When naming your migrations, make them as description as possible. The migration generator will detect the first word in your migration name and do its best to determine how to proceed. As such, for If you instead use a migration name along the lines of
This will prepare the following boilerplate: <?php
use Illuminate\Database\Migrations\Migration;
class AddUserIdToPostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function($table)
{
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function($table)
{
});
}
} Notice how, this time, we're not doing KeywordsWhen writing migration names, use the following keywords to provide hints for the generator.
Generating SchemaThis is pretty nice, but let's take things a step further and also generate the schema, using the
Before we decipher this new option, let's see the output: <?php
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}
} Nice! A few things to notice here:
To declare fields, use a comma-separated list of key:value pairs, where
As a final demonstration, let's run a migration to remove the
This time, as we're using the "remove" keyword, the generator understands that it should drop a column, and add it back in the <?php
use Illuminate\Database\Migrations\Migration;
class RemoveCompletedFromTasksTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tasks', function($table)
{
$table->dropColumn('completed');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tasks', function($table)
{
$table->boolean('completed');
});
}
} Models
This will create the file, <?php
class Post extends Eloquent {
} Tests
This will generate the file, <?php
class PostsTest extends TestCase {
public function test()
{
}
} Should you need to place this file within a subdirectory (or somewhere else in the
Now, If you pass a <?php
class PostsTest extends TestCase {
public function testAll()
{
$response = $this->call('GET', 'posts');
$this->assertTrue($response->isOk());
}
public function testShow()
{
$response = $this->call('GET', 'posts/1');
$this->assertTrue($response->isOk());
}
public function testCreate()
{
$response = $this->call('GET', 'posts/create');
$this->assertTrue($response->isOk());
}
public function testEdit()
{
$response = $this->call('GET', 'posts/1/edit');
$this->assertTrue($response->isOk());
}
} Views
This command will generate
As with all of the commands, you may specify a
Now, we get: SeedsLaravel 4 provides us with a flexible way to seed new tables.
Set the argument to the name of the table that you'd like a seed file for. This will generate <?php
class DogsTableSeeder extends Seeder {
public function run()
{
$Dogs = [
];
DB::table('Dogs')->insert($Dogs);
}
} You're of course free to adjust this, if needed. To fully seed the
ResourcesThink of the resource generator as the big enchilda. It calls all of its sibling generate commands. Assuming the following command:
The following actions will take place:
WorkflowLet's create a resource for displaying dogs in a restful way.
Next, we'll seed this new
We can't forget to call this new seed file from the master
Now, we migrate the database and seed the
Finally, let's display these two dogs, when accessing the
The last step is to update the view to display each of the posts that was passed to it. Open
Okay, okay, we're not using a layout file with the proper HTML. Who cares; this is just an example, fool. Anyhow, we're all set. Run the server, and browse to
Isn't that way faster than manually doing all of that writing? To finish up, let's run the tests to make sure that everything is working, as expected.
And...it's green! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论