Laravel-Bootstrap Starter网站内容管理

时间:2014-10-27 21:13:05

标签: php laravel-4 artisan

我目前正在使用初学者网站: https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site

我将博客文章中的任何html转换为文本。例如,tag hi标签(应该在标签周围有括号)在div中转换为hi。我希望它只在像div这样的标签中输出hi 这是控制器

<?php

class AdminBlogsController extends AdminController {


    /**
     * Post Model
     * @var Post
     */
    protected $post;

    /**
     * Inject the models.
     * @param Post $post
     */
    public function __construct(Post $post)
    {
        parent::__construct();
        $this->post = $post;
    }

    /**
     * Show a list of all the blog posts.
     *
     * @return View
     */
    public function getIndex()
    {
        // Title
        $title = Lang::get('admin/blogs/title.blog_management');

        // Grab all the blog posts
        $posts = $this->post;

        // Show the page
        return View::make('admin/blogs/index', compact('posts', 'title'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function getCreate()
    {
        // Title
        $title = Lang::get('admin/blogs/title.create_a_new_blog');

        // Show the page
        return View::make('admin/blogs/create_edit', compact('title'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postCreate()
    {
        // Declare the rules for the form validation
        $rules = array(
            'title'   => 'required|min:3',
            'content' => 'required|min:3'
        );

        // Validate the inputs
        $validator = Validator::make(Input::all(), $rules);

        // Check if the form validates with success
        if ($validator->passes())
        {
            // Create a new blog post
            $user = Auth::user();

            // Update the blog post data
            $this->post->title            = Input::get('title');
            $this->post->slug             = Str::slug(Input::get('title'));
            $this->post->content          = Input::get('content');
            $this->post->meta_title       = Input::get('meta-title');
            $this->post->meta_description = Input::get('meta-description');
            $this->post->meta_keywords    = Input::get('meta-keywords');
            $this->post->user_id          = $user->id;

            // Was the blog post created?
            if($this->post->save())
            {
                // Redirect to the new blog post page
                return Redirect::to('admin/blogs/' . $this->post->id . '/edit')->with('success', Lang::get('admin/blogs/messages.create.success'));
            }

            // Redirect to the blog post create page
            return Redirect::to('admin/blogs/create')->with('error', Lang::get('admin/blogs/messages.create.error'));
        }

        // Form validation failed
        return Redirect::to('admin/blogs/create')->withInput()->withErrors($validator);
    }

    /**
     * Display the specified resource.
     *
     * @param $post
     * @return Response
     */
    public function getShow($post)
    {
        // redirect to the frontend
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param $post
     * @return Response
     */
    public function getEdit($post)
    {
        // Title
        $title = Lang::get('admin/blogs/title.blog_update');

        // Show the page
        return View::make('admin/blogs/create_edit', compact('post', 'title'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param $post
     * @return Response
     */
    public function postEdit($post)
    {

        // Declare the rules for the form validation
        $rules = array(
            'title'   => 'required|min:3',
            'content' => 'required|min:3'
        );

        // Validate the inputs
        $validator = Validator::make(Input::all(), $rules);

        // Check if the form validates with success
        if ($validator->passes())
        {
            // Update the blog post data
            $post->title            = Input::get('title');
            $post->slug             = Str::slug(Input::get('title'));
            $post->content          = Input::get('content');
            $post->meta_title       = Input::get('meta-title');
            $post->meta_description = Input::get('meta-description');
            $post->meta_keywords    = Input::get('meta-keywords');

            // Was the blog post updated?
            if($post->save())
            {
                // Redirect to the new blog post page
                return Redirect::to('admin/blogs/' . $post->id . '/edit')->with('success', Lang::get('admin/blogs/messages.update.success'));
            }

            // Redirect to the blogs post management page
            return Redirect::to('admin/blogs/' . $post->id . '/edit')->with('error', Lang::get('admin/blogs/messages.update.error'));
        }

        // Form validation failed
        return Redirect::to('admin/blogs/' . $post->id . '/edit')->withInput()->withErrors($validator);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param $post
     * @return Response
     */
    public function getDelete($post)
    {
        // Title
        $title = Lang::get('admin/blogs/title.blog_delete');

        // Show the page
        return View::make('admin/blogs/delete', compact('post', 'title'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param $post
     * @return Response
     */
    public function postDelete($post)
    {
        // Declare the rules for the form validation
        $rules = array(
            'id' => 'required|integer'
        );

        // Validate the inputs
        $validator = Validator::make(Input::all(), $rules);

        // Check if the form validates with success
        if ($validator->passes())
        {
            $id = $post->id;
            $post->delete();

            // Was the blog post deleted?
            $post = Post::find($id);
            if(empty($post))
            {
                // Redirect to the blog posts management page
                return Redirect::to('admin/blogs')->with('success', Lang::get('admin/blogs/messages.delete.success'));
            }
        }
        // There was a problem deleting the blog post
        return Redirect::to('admin/blogs')->with('error', Lang::get('admin/blogs/messages.delete.error'));
    }

    /**
     * Show a list of all the blog posts formatted for Datatables.
     *
     * @return Datatables JSON
     */
    public function getData()
    {
        $posts = Post::select(array('posts.id', 'posts.title', 'posts.id as comments', 'posts.created_at'));

        return Datatables::of($posts)

        ->edit_column('comments', '{{ DB::table(\'comments\')->where(\'post_id\', \'=\', $id)->count() }}')

        ->add_column('actions', '<a href="{{{ URL::to(\'admin/blogs/\' . $id . \'/edit\' ) }}}" class="btn btn-default btn-xs iframe" >{{{ Lang::get(\'button.edit\') }}}</a>
                <a href="{{{ URL::to(\'admin/blogs/\' . $id . \'/delete\' ) }}}" class="btn btn-xs btn-danger iframe">{{{ Lang::get(\'button.delete\') }}}</a>
            ')

        ->remove_column('id')

        ->make();
    }

}

这是模型

<?php

使用Illuminate \ Support \ Facades \ URL;

类帖子扩展了Eloquent {

/**
 * Deletes a blog post and all
 * the associated comments.
 *
 * @return bool
 */
public function delete()
{
    // Delete the comments
    $this->comments()->delete();

    // Delete the blog post
    return parent::delete();
}

/**
 * Returns a formatted post content entry,
 * this ensures that line breaks are returned.
 *
 * @return string
 */
public function content()
{
    return nl2br($this->content);
}

/**
 * Get the post's author.
 *
 * @return User
 */
public function author()
{
    return $this->belongsTo('User', 'user_id');
}

/**
 * Get the post's meta_description.
 *
 * @return string
 */
public function meta_description()
{
    return $this->meta_description;
}

/**
 * Get the post's meta_keywords.
 *
 * @return string
 */
public function meta_keywords()
{
    return $this->meta_keywords;
}

/**
 * Get the post's comments.
 *
 * @return array
 */
public function comments()
{
    return $this->hasMany('Comment');
}

/**
 * Get the date the post was created.
 *
 * @param \Carbon|null $date
 * @return string
 */
public function date($date=null)
{
    if(is_null($date)) {
        $date = $this->created_at;
    }

    return String::date($date);
}

/**
 * Get the URL to the post.
 *
 * @return string
 */
public function url()
{
    return Url::to($this->slug);
}

/**
 * Returns the date of the blog post creation,
 * on a good and more readable format :)
 *
 * @return string
 */
public function created_at()
{
    return $this->date($this->created_at);
}

/**
 * Returns the date of the blog post last update,
 * on a good and more readable format :)
 *
 * @return string
 */
public function updated_at()
{
    return $this->date($this->updated_at);
}

}

先谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

这里有两个解决方案:

  • 您可以找到正在编码内容的文件,并删除正在进行编码的代码
  • 或者当您需要输出已编码的值时,只需使用HTML::decode()对其进行解码即可。因此,对于编码的帖子内容,您可以在视图HTML::decode($post->content)中书写。

答案 1 :(得分:1)

这是因为你正在使用的视图中的输出正在通过刀片花括号{{{ }}}运行htmlentities,意思是;

<div>hi </div> 

转换为

<&lt;div&gt;hi &lt;/div&gt;

要防止这种情况并在帖子中允许html,请将{{{ }}}更改为{{ }}