laravel:redirect() - > back() - > with()不发送任何消息

时间:2016-02-29 02:45:09

标签: php redirect laravel-5 routes url-redirection

我根据提供给this question 的答案按照指示行事 但对我来说无法正常工作。所以我重新提出了这个问题。 我的控制器是

public function save() {

        $med_group = MedicineGroup::create(Request::all());

        if ($med_group) {
            $this->setServerMessage('MedicineGroup created successfully');
            return Redirect::to('admin/user/create')->with('flashmessage',$this->getServerMessage());

        }
    }

我在Controller.php中创建了setServerMessage()和getServerMessage()

public function setServerMessage($messagearray) {
        if (is_array($messagearray)) {
            $type = $messagearray['type'];
            $html = "<div style='height:auto;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-{$type}'>{$messagearray[0]}</div>";
        } else {
            $html = "<div style='height:33px;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-info'>{$messagearray}</div>";
        }
        $temp = '';
        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
        }
        \Session::put('SERVER_MESSAGE', $temp . $html);
    }

public function getServerMessage() {

        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
            \Session::forget('SERVER_MESSAGE');
            return $temp;
        } else
            return "";
    }

我的观点设置如下

<div class="box-footer text-right">
                @include('flash')
                <input type="submit" class="btn btn-success" value='Save'>
                <input type="reset" class="btn btn-primary" value='Reset' />
                <a href="#" class="btn btn-danger">Cancel</a>
            </div>

在我的flash.blade.php中写了

@if(isset($flashmessage))
   {!! $flashmessage !!}
@endif

我错过了什么?我跟着this site too,但我不能在我看来发短信。

3 个答案:

答案 0 :(得分:1)

设置闪光信息,然后重定向到所需的路线

<强>控制器:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Limit, Description) values ('101','Deluxe',350,2,'')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at hotelBillingAndReservation.addRoom$2.actionPerformed(addRoom.java:115)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

现在获取视图刀片文件中的消息

<强>刀片

  session()->flash('msg', 'Successfully done the operation.');
  return Redirect::to('admin/user/create');

答案 1 :(得分:0)

试试这个,我正在替换你的整个代码,你可以根据需要改变它:

public function save()
{
    $med_group = MedicineGroup::create(Request::all());

    if ($med_group) {
        //$this->setServerMessage('MedicineGroup created successfully');

        // flash method to be used when just printing the message
        // on the screen. Link below
        \Session::flash('key', 'Your message here...');

        return Redirect::to('admin/user/create');

    }
}

在您的视图文件中:

@if(Session::has('key))
    {{ Session::get('key') }}
@endif

Link for more information

答案 2 :(得分:0)

every other thing seems to be fine except for your layout 

laravel would escape the variable you are passing into the session while using this 

@if(isset($flashmessage))
   **{!! $flashmessage !!}**
@endif

you should do this instead 

@if(isset($flashmessage))
   **{{ $flashmessage }}**
@endif