Nova 是什么我就不多说了,反正用过以后就不会再去用别的后台框架了。
下面我将用一系列文章来实践实践nova,有什么问题欢迎大家指点交流。

入门

首先你必须到nova的官网 注册 一个账号。
一旦注册好了,你必须买一个 license 来用nova的代码,这个我就不演示了。
开发者是 99美金,团队是 199美金。愿意支持作者的,喜欢nova的我觉得都要去买一下,最好不要从别人手里买,因为程序要是留点后门什么的那就得不偿失了。

买好 license 后,就把nova下载到你自己的电脑上。

下一步,创建一个项目:下面两个命令随便用哪个
 laravel new <your-project-name> 
composer create-project laravel/laravel <your-project-name>
我习惯用第二个,还可以指定laravel的版本,更重要的是速度换成国内源超快。
创建好了项目后把nova目录直接移动到你项目的根目录(其他目录也行)

项目安装

配置你的 composer.json 来识别 Nova:

"repositories": [
    {
        "type": "path",
        "url": "./nova"
    }
],

然后把 Nova 添加到  composer.jsonrequire 如下:

"require": {
    "php": "^7.1.3",
    "fideloper/proxy": "^4.0",
    "laravel/framework": "5.6.*",
    "laravel/nova": "*"
},

运行安装命令前先在 .env文件里面配置下数据库信息。千万先配置好,要不然出错了就难受了。

使用下面的命令在 terminal 里安装 Nova:
(用windows的朋友我就不知道怎么弄了,自己研究)

composer update

php artisan nova:install

php artisan migrate

好了,现在我们开始访问 /nova 路由看看它的登陆界面把。

nova login

添加第一个用户

为了登录我也是拼了,在终端用这个命令 php artisan nova:user 创建我们的系统。

nova登录界面

我们来建个博客玩玩

让我们像建其他laravel项目一样创建这个博客把。 用 php artisan make:model Post -a 给我们的博客创建 Post 模型。现在让我们来创建迁移文件,加些字段。

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->text('title');
        $table->text('subtitle');
        $table->text('body');
        $table->dateTime('published_on');
        $table->timestamps();
    });
}

运行 php artisan migrate 把表加到数据库里面。做完后进入  Post.php 文件配置模型关系。
注意下我们正确设置一个datetime 列。

看下 Post.php 文件如下:

class Post extends Model
{
    protected $dates = [
      'published_on',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

现在我们在 User 模型里面定义反转关系。

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

好了,做完这些,剩下的就是该做Nova方面的事了。

使用 Nova

使用nova的时候先看下它的 resources的资料把。你想用nova管理的每个模型在nova里面都有一个相应的 resource
为了生成 Post 的 resource 我们运行nova:resource Post命令。现在看下你的app\Nova 目录,是不是多了些什么?
是吧,默认 User resource 已经在里面了,现在多出来的是你刚才创建的 Post Resource ,你要配置的大部分和nova相关的东西都会在这里进行管理。
好了,现在我们准备开始做一些 CRUD 的操作,做之前我们需要在我们的 Post Resource 里面添加一些field()。这就是告诉我们的nova我们的模型是如何定义我们表的字段的。

Nova 有 许多不同的 字段给我们用,没事多看看文档把。我这里就用些简单的吧:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        BelongsTo::make('User')->rules('required'),
        Text::make('Title')->rules('required')->sortable(),
        Text::make('Subtitle')->rules('required')->hideFromIndex(),
        Markdown::make('Body')->rules('required'),
        DateTime::make('Published On')->rules('required'),
    ];
}

我们通过 subtitle 这个字段来解释下 field 到底是干什么的。

Text::make('Subtitle')->rules('required')->hideFromIndex(),

Text 告诉我们要生成什么类型的字段。然后这个标签就是我们传递给 make() 静态方法的。标签需要按蛇形写法去匹配数据表里的列。

我们也可以使用 rules()方法加一些验证。 本例中,我们验证 Subtitle 是一个必填字段。

最后, 我们要调整下字段 显示 的区域,用hideFromIndex() 方法就行。这会阻止字段在列表页显示。 当然有些字段处理起来有点不一样,(i.e. Markdown)。当然,大多数时候,如果你不用隐藏方法,哪里都会显示该字段。

就这么简单,我们构建了我们博客的整个CMS。 如果我们回到控制台,我们就可以看到一个新的Posts 菜单了(位于Resources下面)。

点击一下 Posts看看首页都显示个啥?

post 页面

我们开始创建一篇文章。点击Create Post创建。

如果我们直接点提交,什么都不输入的话,你就会看到很多验证的错误提示。

Errors

一旦我们正常提交,你就可以切换到文章的详情页看看了。

Post Details

最后回到文章的首页,再瞧瞧什么样,刚才创建的文章是不是在列表中了,哈哈。

Post List

就这样,我们为博客构建了一个CMS。

现在,虽然这样可以解决管理方面的问题,但我们仍然需要让我们的用户可以查看我们的帖子。
该怎么办呢?

回到 Laravel

为了完成这个博客系统,我们进入 PostController 控制器并且设置一些视图。本例中我只关index() 和show(),所以先把其他的方法删掉吧,我是天蝎座,貌似也有点强迫症。

现在,我们将快速获取正确的数据并将用户发送到一些新视图。

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::orderByDesc('published_on')->get();

        return view('posts', compact('posts'));
    }

    public function show(Post $post)
    {
        return view('post', compact('post'));
    }
}

下面我们进入 web.php 文件来定义我们的路由,如下(记得把以前的代码删掉):

Route::get('/', 'PostController@index');
Route::get('/posts/{post}', 'PostController@show');

最后重命名 welcome.blade.php 为 posts.blade.php. 把代码改成下面这样的:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 200;
                height: 100vh;
                margin: 0;
            }

            .m-b-md {
                display: block;
                margin-bottom: 60px;
            }
        </style>
    </head>
    <body>
        <div>
            @foreach($posts as $post)
                <a href="/posts/{{$post->id}}" class="m-b-md">
                    <h1>{{$post->title}}</h1>
                    <h3>{{$post->subtitle}}</h3>
                    <h4>{{$post->published_on}}</h4>
                </a>
            @endforeach
        </div>
    </body>
</html>

现在让我们发布一些博文把,随便输入。

最后,复制posts.blade.php文件并且重命名为 post.blade.php。把里面的代码改成下面的:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 200;
                height: 100vh;
                margin: 0;
            }
        </style>
    </head>
    <body>
        <div>
            <h1>{{$post->title}}</h1>
            <h3>{{$post->subtitle}}</h3>
            <h4>Written by {{$post->user->name}} on {{$post->published_on}}</h4>
        </div>
        <article>{{$post->body}}</article>
    </body>
</html>

看下把,我们终于可以看到每篇文章的详细内容了。

总结

我不知道你是咋办的,但是这是我用过的最快的创建一个博客的办法。(老外就喜欢瞎说,我都是直接用开源博客程序)。不过,nova 真的没得说。
好了,这次先到这,下一篇文章继续。

Laravel Nova 入门创建一个简单的博客(一)
Tagged on:     

87 thoughts on “Laravel Nova 入门创建一个简单的博客(一)

  • Pingback:Leijingwei's

  • 2019-05-09 at 13:46
    Permalink

    Appreciating the commitment you put into your blog
    and detailed information you provide. It’s good to come across
    a blog every once in a while that isn’t the same old rehashed information.
    Wonderful read! I’ve saved your site and I’m including your RSS feeds
    to my Google account.

    Reply
  • 2019-05-09 at 14:04
    Permalink

    Great information. Lucky me I found your website by chance (stumbleupon).
    I’ve bookmarked it for later!

    Reply
  • 2019-05-09 at 14:11
    Permalink

    This is the perfect website for anybody who hopes to find out about this topic.

    You understand so much its almost hard to argue with you (not
    that I really would want to…HaHa). You certainly put a fresh spin on a
    topic that’s been discussed for ages. Great stuff, just wonderful!

    Reply
  • 2019-05-09 at 14:45
    Permalink

    Definitely consider that which you stated. Your favourite reason seemed to be on the internet the easiest factor to be mindful of.
    I say to you, I definitely get irked whilst other people think
    about worries that they plainly do not understand about.
    You managed to hit the nail upon the highest and also outlined out the entire thing with no need side-effects , people can take a signal.
    Will likely be again to get more. Thanks

    Reply
  • 2019-05-09 at 16:39
    Permalink

    Aw, this was an incredibly good post. Spending some time and actual effort to make a top
    notch article… but what can I say… I hesitate a lot and never manage to get
    anything done.

    Reply
  • 2019-05-09 at 17:09
    Permalink

    Hey there, I think your blog might be having browser compatibility issues.

    When I look at your blog in Ie, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to give you a quick heads up!

    Other then that, very good blog!

    Reply
  • 2019-05-09 at 18:37
    Permalink

    naturally like your web site but you need to check the spelling on quite a few of your posts.
    Many of them are rife with spelling issues and I find it very bothersome to tell the truth on the other hand I will certainly come back again.

    Reply
  • 2019-05-09 at 19:15
    Permalink

    you are really a just right webmaster. The site loading velocity
    is incredible. It seems that you are doing any unique trick.
    In addition, The contents are masterpiece. you’ve done a
    fantastic task on this subject!

    Reply
  • 2019-05-09 at 19:31
    Permalink

    Definitely consider that that you stated. Your favorite reason appeared
    to be at the web the easiest factor to take into account of.
    I say to you, I definitely get irked at the same time as other people consider worries that they just do not
    recognise about. You controlled to hit the nail upon the top
    and outlined out the whole thing without having side-effects ,
    people can take a signal. Will probably be back
    to get more. Thanks

    Reply
  • 2019-05-09 at 21:01
    Permalink

    Today, I went to the beach with my kids. I found a sea shell and gave it to my
    4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the
    shell to her ear and screamed. There was a hermit
    crab inside and it pinched her ear. She never wants to
    go back! LoL I know this is completely off topic but I
    had to tell someone!

    Reply
  • 2019-05-09 at 21:47
    Permalink

    Fine way of telling, and good article to obtain information on the topic of my presentation focus,
    which i am going to deliver in academy.

    Reply
  • 2019-05-11 at 14:06
    Permalink

    Thank you a bunch for sharing this with all folks you really recognize what
    you’re talking approximately! Bookmarked. Kindly also consult with my
    website =). We can have a hyperlink exchange agreement among us!

    Reply
  • 2019-05-11 at 17:14
    Permalink

    you’re truly a excellent webmaster. The site loading velocity is incredible.
    It kind of feels that you’re doing any unique trick.
    Furthermore, The contents are masterpiece. you have done a fantastic task in this subject!

    Reply
  • 2019-05-11 at 17:40
    Permalink

    I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you?
    Plz reply as I’m looking to design my own blog and
    would like to find out where u got this from.
    many thanks

    Reply
  • 2019-05-11 at 17:50
    Permalink

    Thanks for sharing your info. I really appreciate your efforts and
    I will be waiting for your further post thanks once
    again.

    Reply
  • 2019-05-11 at 18:02
    Permalink

    Remarkable! Its genuinely amazing piece of writing, I
    have got much clear idea regarding from this article.

    Reply
  • 2019-05-11 at 20:50
    Permalink

    Thanks for sharing your thoughts. I truly appreciate
    your efforts and I will be waiting for your further post
    thank you once again.

    Reply
  • 2019-05-11 at 21:09
    Permalink

    Magnificent beat ! I would like to apprentice at the same
    time as you amend your site, how can i subscribe for
    a weblog website? The account helped me a acceptable deal.
    I have been tiny bit acquainted of this your broadcast provided vibrant transparent idea

    Reply
  • 2019-05-11 at 22:59
    Permalink

    Have you ever thought about publishing an ebook or guest authoring on other websites?

    I have a blog centered on the same subjects you discuss and would really like to have you share some stories/information. I know my readers
    would appreciate your work. If you’re even remotely interested, feel
    free to shoot me an e-mail.

    Reply
  • 2019-05-11 at 23:05
    Permalink

    Attractive component of content. I just stumbled upon your weblog and in accession capital to assert that I acquire in fact loved account your
    blog posts. Any way I’ll be subscribing in your feeds or even I fulfillment you get right
    of entry to consistently fast.

    Reply
  • 2019-05-11 at 23:32
    Permalink

    Woah! I’m really loving the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s very difficult
    to get that “perfect balance” between usability and appearance.
    I must say you’ve done a awesome job with this.
    Also, the blog loads extremely quick for me on Opera.
    Outstanding Blog!

    Reply
  • 2019-05-11 at 23:54
    Permalink

    Hi my loved one! I wish to say that this article is amazing, nice written and
    come with approximately all important infos.
    I’d like to look more posts like this .

    Reply
  • 2019-05-11 at 23:55
    Permalink

    Fantastic items from you, man. I’ve have in mind your stuff prior to and you are
    just extremely wonderful. I actually like what you have received here, certainly like what
    you’re saying and the best way wherein you are saying
    it. You make it entertaining and you continue to care for to keep it wise.
    I can not wait to learn far more from you. That is really a wonderful
    site.

    Reply
  • 2019-05-12 at 00:39
    Permalink

    We’re a group of volunteers and starting a new scheme
    in our community. Your site offered us with valuable information to work on. You’ve done an impressive
    job and our whole community will be grateful to you.

    Reply
  • 2019-05-12 at 00:54
    Permalink

    This site was… how do I say it? Relevant!!

    Finally I’ve found something that helped me. Thanks
    a lot!

    Reply
  • 2019-05-12 at 01:21
    Permalink

    It’s actually a great and useful piece of info. I’m happy that you just shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.

    Reply
  • 2019-05-12 at 01:29
    Permalink

    I believe that is among the most important info for me. And i am glad studying your article.
    However want to statement on some basic things, The web
    site style is wonderful, the articles is actually nice :
    D. Good process, cheers

    Reply
  • 2019-05-12 at 01:35
    Permalink

    Post writing is also a excitement, if you be familiar with afterward you can write if not it is difficult to write.

    Reply
  • 2019-05-12 at 15:05
    Permalink

    It’s in reality a nice and useful piece of info.
    I’m happy that you shared this helpful information with us.
    Please stay us up to date like this. Thank you for sharing.

    Reply
  • 2019-05-13 at 18:01
    Permalink

    If some one desires to be updated with hottest technologies then he must be visit this web page and be up to
    date daily.

    Reply
  • 2019-05-13 at 18:14
    Permalink

    I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your
    theme? Outstanding work!

    Reply
  • 2019-05-14 at 08:35
    Permalink

    It’s enormous that you are getting ideas from this piece of writing as well
    as from our discussion made here.

    Reply
  • 2019-05-14 at 09:15
    Permalink

    No matter if some one searches for his essential thing, thus
    he/she desires to be available that in detail, thus that thing is maintained over here.

    Reply
  • 2019-05-14 at 10:34
    Permalink

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my
    blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some
    time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog
    and I look forward to your new updates.

    Reply
  • 2019-05-14 at 11:05
    Permalink

    I’d like to thank you for the efforts you’ve put in penning this blog.
    I really hope to view the same high-grade content
    from you in the future as well. In truth, your creative writing abilities
    has inspired me to get my very own site now 😉

    Reply
  • 2019-05-14 at 11:36
    Permalink

    I’m really impressed with your writing skills and also with the
    layout for your weblog. Is that this a paid theme or did you customize it your self?
    Anyway keep up the excellent high quality writing, it’s uncommon to peer
    a nice weblog like this one today..

    Reply
  • 2019-05-14 at 13:26
    Permalink

    I do not even know the way I ended up here, however I assumed
    this post was once great. I don’t know who you might
    be but definitely you’re going to a well-known blogger if you aren’t already.
    Cheers!

    Reply
  • 2019-05-14 at 14:50
    Permalink

    Hmm it appears like your site ate my first comment (it
    was super long) so I guess I’ll just sum it up what I had
    written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog writer but I’m still new to everything.
    Do you have any tips for rookie blog writers? I’d certainly appreciate
    it.

    Reply
  • 2019-05-14 at 16:53
    Permalink

    E.g. you spend $50,000 on a renovation then a revaluation provides $100,000 to your own home you may use the additional $50,000 to invest
    in another property.

    Reply
  • 2019-05-15 at 04:09
    Permalink

    2015 En Çok Okunan Yabancı Kitaplar Kitap Internet Satış

    Reply
  • 2019-05-15 at 12:42
    Permalink

    Hi, i believe that i noticed you visited my website so i got here to return the
    favor?.I am trying to in finding issues to improve my web site!I guess its good enough to make use of a
    few of your concepts!!

    Reply
  • 2019-05-15 at 13:02
    Permalink

    Terrific article! This is the type of information that should be shared across the net.
    Shame on Google for not positioning this publish upper!
    Come on over and visit my web site . Thank you =)

    Reply
  • 2019-05-15 at 13:15
    Permalink

    Hi it’s me, I am also visiting this site on a regular basis, this web page is really fastidious
    and the people are really sharing nice thoughts.

    Reply
  • 2019-05-15 at 13:50
    Permalink

    Hello, i think that i saw you visited my site so i came to “return the favor”.I’m
    attempting to find things to enhance my web site!I suppose its ok to use a few of your
    ideas!!

    Reply
  • 2019-05-15 at 14:34
    Permalink

    Very great post. I just stumbled upon your weblog and wished to say that I have truly
    loved surfing around your weblog posts. In any case I’ll be
    subscribing on your rss feed and I am hoping you write again soon!

    Reply
  • 2019-05-15 at 14:50
    Permalink

    Hey! I know this is somewhat off topic but I was wondering if you
    knew where I could locate a captcha plugin for my
    comment form? I’m using the same blog platform
    as yours and I’m having problems finding one? Thanks a lot!

    Reply
  • 2019-05-15 at 15:00
    Permalink

    Hello would you mind sharing which blog platform
    you’re using? I’m going to start my own blog soon but
    I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs and I’m looking
    for something unique. P.S Sorry for getting off-topic
    but I had to ask!

    Reply
  • 2019-05-15 at 15:12
    Permalink

    I’ll immediately grab your rss feed as I can’t to find your email subscription link or newsletter service.
    Do you have any? Kindly permit me recognise in order that I
    may just subscribe. Thanks.

    Reply
  • 2019-05-15 at 16:40
    Permalink

    whoah this blog is great i love studying your articles. Stay up
    the great work! You understand, many persons are hunting round for
    this information, you could aid them greatly.

    Reply
  • 2019-05-15 at 21:36
    Permalink

    Great ? I should definitely pronounce, impressed with your
    web site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access.
    I recently found what I hoped for before you
    know it at all. Quite unusual. Is likely to appreciate it for those who add forums or anything, site theme
    . a tones way for your customer to communicate. Nice task.

    Reply
  • 2019-05-17 at 11:34
    Permalink

    I really like your blog.. very nice colors & theme. Did you
    design this website yourself or did you hire someone to
    do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where
    u got this from. thanks

    Reply
  • 2019-05-17 at 11:43
    Permalink

    What’s up to all, because I am really eager of reading this web
    site’s post to be updated on a regular basis. It carries good stuff.

    Reply
  • 2019-05-18 at 10:56
    Permalink

    These are in fact wonderful ideas in concerning blogging.
    You have touched some nice points here. Any way keep up wrinting.

    Reply
  • 2019-05-18 at 12:48
    Permalink

    I love what you guys are up too. This kind of clever work and
    exposure! Keep up the wonderful works guys I’ve added you
    guys to my personal blogroll.

    Reply
  • 2019-05-18 at 13:27
    Permalink

    I relish, lead to I discovered exactly what I used to be taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man.
    Have a nice day. Bye

    Reply
  • 2019-05-18 at 14:09
    Permalink

    Excellent pieces. Keep posting such kind of information on your blog.
    Im really impressed by it.
    Hey there, You’ve done an incredible job. I will definitely digg it and for my part suggest
    to my friends. I am confident they will be benefited from this web site.

    Reply
  • 2019-05-18 at 14:20
    Permalink

    Hi there! This post could not be written any better!
    Reading this post reminds me of my good old room mate!

    He always kept chatting about this. I will forward this article to him.

    Pretty sure he will have a good read. Thanks for sharing!

    Reply

Leave a Reply to www.vintagehouserestaurant.com Cancel reply

Your email address will not be published. Required fields are marked *