0

我目前正在这里使用入门网站: https ://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site

我作为博文发布的任何 html 都会转换为文本。例如,标签 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;

类 Post 扩展 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);
}

}

提前感谢您的帮助!

4

2 回答 2

1

您在这里有两个解决方案:

  • 您可以找到正在对内容进行编码的文件并删除正在执行编码的代码
  • 或者当您需要输出已编码的值时,只需使用HTML::decode(). 因此,对于编码的帖子内容,您可以在 yout view 中编写HTML::decode($post->content)
于 2014-10-27T22:31:59.017 回答
1

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

<div>hi </div> 

被转换成

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

为了防止这种情况并允许在帖子中使用 html,{{{ }}}请将{{ }}.

于 2014-10-28T14:27:05.977 回答