7 Commits 8a79ede09e ... 4af0c238a9

Auteur SHA1 Message Date
  Artem Petrov 4af0c238a9 Merge branch 'develop' into feature-posts il y a 5 ans
  Artem Petrov 132a44ce13 [#] change homepage message il y a 5 ans
  Artem Petrov b11160c3e8 [#] change homepage message il y a 5 ans
  Artem Petrov a80faefce7 [+] add editing posts il y a 5 ans
  Artem Petrov 316775b801 [+] add creating posts il y a 5 ans
  Artem Petrov 7c68693a31 add creating posts il y a 5 ans
  Artem Petrov ebf30862c8 [+] add posts skeleton il y a 5 ans

+ 96 - 0
app/Http/Controllers/PostsController.php

@@ -0,0 +1,96 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 21.09.18
+ * Time: 21:00
+ */
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\PostRequest;
+use App\Post;
+use Illuminate\Support\Facades\Auth;
+
+class PostsController extends Controller
+{
+    /**
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function index()
+    {
+        $posts = Post::all();
+
+        return view('posts.index', [
+            'posts' => $posts,
+        ]);
+    }
+
+    /**
+     * @param Post $post
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function view(Post $post)
+    {
+        return view('posts.view', [
+            'post' => $post,
+        ]);
+    }
+
+    /**
+     * @param Post $post
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function create()
+    {
+        return view('posts.create');
+    }
+
+    /**
+     * @param PostRequest $request
+     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     */
+    public function store(PostRequest $request)
+    {
+        $post = new Post();
+        $post->fill($request->all() + [
+                'user_id' => Auth::id(),
+            ]);
+        $post->save();
+
+        return redirect(route('posts.index'));
+    }
+
+    /**
+     * @param Post $post
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function update(Post $post)
+    {
+        $user = Auth::user();
+        if (!$user->owns($post)) {
+            return redirect(route('posts.index'));
+        }
+
+        return view('posts.update', [
+            'post' => $post,
+        ]);
+    }
+
+    /**
+     * @param PostRequest $request
+     * @param Post $post
+     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     */
+    public function edit(PostRequest $request, Post $post)
+    {
+        $user = Auth::user();
+        if (!$user->owns($post)) {
+            abort(403);
+        }
+        $post->fill($request->all());
+        $post->save();
+
+        return redirect(route('posts.index'));
+    }
+}

+ 34 - 0
app/Http/Requests/PostRequest.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 21.09.18
+ * Time: 21:39
+ */
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class PostRequest extends FormRequest
+{
+
+    /**
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            'title' => 'required|max:100',
+            'description' => 'required|max:5000',
+        ];
+    }
+}

+ 27 - 0
app/Post.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 21.09.18
+ * Time: 20:54
+ */
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Post extends Model
+{
+    protected $fillable = [
+        'user_id',
+        'title',
+        'description',
+    ];
+    /**
+     * Get the user that owns the phone.
+     */
+    public function user()
+    {
+        return $this->belongsTo(User::class);
+    }
+}

+ 2 - 0
app/User.php

@@ -4,10 +4,12 @@ namespace App;
 
 use Illuminate\Notifications\Notifiable;
 use Illuminate\Foundation\Auth\User as Authenticatable;
+use Laratrust\Traits\LaratrustUserTrait;
 
 class User extends Authenticatable
 {
     use Notifiable;
+    use LaratrustUserTrait;
 
     /**
      * The attributes that are mass assignable.

+ 2 - 1
composer.json

@@ -8,7 +8,8 @@
         "php": ">=7.0.0",
         "fideloper/proxy": "~3.3",
         "laravel/framework": "5.5.*",
-        "laravel/tinker": "~1.0"
+        "laravel/tinker": "~1.0",
+        "santigarcor/laratrust": "5.0.*"
     },
     "require-dev": {
         "filp/whoops": "~2.0",

+ 115 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "b7904d07d1e1765a0a199aa11d6301a3",
+    "content-hash": "87c514deb5f77c2a6afc16c500837711",
     "packages": [
         {
             "name": "dnoegel/php-xdg-base-dir",
@@ -407,6 +407,51 @@
             ],
             "time": "2015-04-20T18:58:01+00:00"
         },
+        {
+            "name": "kkszymanowski/traitor",
+            "version": "0.2.5",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/KKSzymanowski/Traitor.git",
+                "reference": "9770fc7de72ff585601dc9c42b31715d9fc40a24"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/KKSzymanowski/Traitor/zipball/9770fc7de72ff585601dc9c42b31715d9fc40a24",
+                "reference": "9770fc7de72ff585601dc9c42b31715d9fc40a24",
+                "shasum": ""
+            },
+            "require": {
+                "nikic/php-parser": "^1.0|^2.0|^3.0|^4.0",
+                "php": ">=5.4"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "~4.1"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Traitor\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Kuba Szymanowski",
+                    "email": "kuba.szymanowski@inf24.pl"
+                }
+            ],
+            "description": "Add a trait use statement to existing PHP class",
+            "keywords": [
+                "add",
+                "php",
+                "trait"
+            ],
+            "time": "2018-04-19T12:24:36+00:00"
+        },
         {
             "name": "laravel/framework",
             "version": "v5.5.43",
@@ -1261,6 +1306,75 @@
             ],
             "time": "2018-07-19T23:38:55+00:00"
         },
+        {
+            "name": "santigarcor/laratrust",
+            "version": "5.0.9",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/santigarcor/laratrust.git",
+                "reference": "526cc3e8970c35b97c71a7a6d8b63c2073568bb1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/santigarcor/laratrust/zipball/526cc3e8970c35b97c71a7a6d8b63c2073568bb1",
+                "reference": "526cc3e8970c35b97c71a7a6d8b63c2073568bb1",
+                "shasum": ""
+            },
+            "require": {
+                "illuminate/auth": "~5.2",
+                "illuminate/cache": "~5.2",
+                "illuminate/console": "~5.2",
+                "illuminate/database": "^5.2.32",
+                "illuminate/support": "~5.2",
+                "kkszymanowski/traitor": "^0.2.0",
+                "php": ">=5.5.9"
+            },
+            "require-dev": {
+                "mockery/mockery": ">=0.9.9",
+                "orchestra/testbench": "~3.2",
+                "phpunit/phpunit": ">=4.1"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "Laratrust\\LaratrustServiceProvider"
+                    ],
+                    "aliases": {
+                        "Laratrust": "Laratrust\\LaratrustFacade"
+                    }
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Laratrust\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Santiago Garcia",
+                    "homepage": "http://santigarcor.me"
+                }
+            ],
+            "description": "This package provides a flexible way to add Role-based Permissions to Laravel",
+            "keywords": [
+                "Teams",
+                "acl",
+                "authorization",
+                "laratrust",
+                "laravel",
+                "multiusers",
+                "permissions",
+                "php",
+                "rbac",
+                "roles"
+            ],
+            "time": "2018-03-05T13:21:52+00:00"
+        },
         {
             "name": "swiftmailer/swiftmailer",
             "version": "v6.1.3",

+ 36 - 0
database/migrations/2018_09_21_175620_create_posts_table.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePostsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('posts', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('title');
+            $table->text('description');
+            $table->integer('user_id')->unsigned();
+            $table->timestamps();
+
+            $table->foreign('user_id')->references('id')->on('users');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('posts');
+    }
+}

+ 21 - 0
resources/views/posts/create.blade.php

@@ -0,0 +1,21 @@
+<form method="post" action="{{route('posts.store')}}">
+
+    <input type="hidden" name="_token" value="{{ csrf_token() }}">
+    <div>
+    <label for="title">Title:</label>
+    <input id='title' name="title" value="{{old('title')}}"/>
+        @if($errors->has('title'))
+            <div>{{ $errors->first('description') }}</div>
+        @endif
+    </div>
+    <div>
+        <label for="description">Description:</label>
+
+        <input id='description' name="description" value="{{old('description')}}"/>
+        @if($errors->has('description'))
+            <div>{{ $errors->first('description') }}</div>
+        @endif
+    </div>
+
+    <button>ТЫЦ</button>
+</form>

+ 14 - 0
resources/views/posts/index.blade.php

@@ -0,0 +1,14 @@
+@auth
+    <a href="{{route('posts.create')}}">
+    Create new post
+</a>
+@endauth
+
+@foreach($posts as $post)
+    <div class="post">
+
+        <h1><a href="/posts/{{$post->id}}">{{$post->title}}</a></h1>
+        <a href="{{route('posts.update', ['post' => $post])}}">Edit</a>
+        <div>{{$post->description}}</div>
+    </div>
+@endforeach

+ 22 - 0
resources/views/posts/update.blade.php

@@ -0,0 +1,22 @@
+<form method="post" action="{{route('posts.edit', ['post' => $post])}}">
+
+    <input type="hidden" name="_method" value="PUT" />
+    <input type="hidden" name="_token" value="{{ csrf_token() }}">
+
+    <div>
+    <label for="title">Title:</label>
+    <input id='title' name="title" value="{{old('title') ?? $post->title}}"/>
+        @if($errors->has('title'))
+            <div>{{ $errors->first('description') }}</div>
+        @endif
+    </div>
+    <div>
+        <label for="description">Description:</label>
+        <input id='description' name="description" value="{{old('description') ?? $post->description}}"/>
+        @if($errors->has('description'))
+            <div>{{ $errors->first('description') }}</div>
+        @endif
+    </div>
+
+    <button>ТЫЦ</button>
+</form>

+ 5 - 0
resources/views/posts/view.blade.php

@@ -0,0 +1,5 @@
+    <div class="post">
+
+        <h1>{{$post->title}}</h1>
+        <div>{{$post->description}}</div>
+    </div>

+ 1 - 1
resources/views/welcome.blade.php

@@ -69,7 +69,7 @@
             @if (Route::has('login'))
                 <div class="top-right links">
                     @auth
-                        <a href="{{ url('/home') }}">Home</a>
+                        <a href="{{ url('/home') }}">Some another text</a>
                     @else
                         <a href="{{ route('login') }}">Login</a>
                         <a href="{{ route('register') }}">Register</a>

+ 13 - 0
routes/web.php

@@ -18,3 +18,16 @@ Route::get('/', function () {
 Auth::routes();
 
 Route::get('/home', 'HomeController@index')->name('home');
+Route::get('/posts', 'PostsController@index')->name('posts.index');
+
+Route::middleware('auth')->group(function () {
+    Route::get('/posts/create', 'PostsController@create')->name('posts.create');
+    Route::post('/posts', 'PostsController@store')->name('posts.store');
+    Route::get('/posts/update/{post}', 'PostsController@update')->name('posts.update');
+    Route::put('/posts/update/{post}', 'PostsController@edit')->name('posts.edit');
+});
+
+Route::get('/posts/{post}', 'PostsController@view')->name('posts.view');
+
+
+