【问题标题】:laravel route and controllerlaravel 路由和控制器
【发布时间】:2017-03-05 00:31:21
【问题描述】:

我是一个新的 laravel 用户和一个管理页面,它执行更新删除和插入我的问题是我不知道如何在路由中调用这个函数。 注意:所有这些选项都在一个页面上工作(管理员)。

所以请谁能帮帮我?!

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;
class BlogPostController extends Controller
{ 
/**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(){
    $date = date('Y-m-d');
        $time = time('H:i:s'); 
        /*$sections = ['History' => 'history.png','Electronics' => 'electronics.png','Electrical' => 'electrical.png','Science' => 'science.png',
                    'Art'=>'ARt.png','Database'=>'database.png','Irrigation'=>'irrigation.png','Novel'=>'Novel.png','Style'=>'Stsyle.png']; 
        */
        $sections = DB ::table('sections')->get();
        return view('libraryViewsContainer.library')->withSections($sections)->withDate($date)->withTime($time);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create(){
        //return View::make('posts.create');
        return '<center><h1>Creating new section in the library!</h1></center>';
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store( Request $request){
        $section_name = $request->input('section_name');
        $file = $request->file('image');
        $destinationPath = 'images';
        $filename = $file->getClientOriginalName();
        $file->move($destinationPath,$filename);

        DB ::table('sections')->insert(['section_name'=>$section_name,'image_name'=>$filename]);
        return redirect('admin');
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id){
    //  $post = Post::find($id);

    //      return View::make('posts.show')->with('post', $post);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id){
    //  $post = Post::find($id);

    //      return View::make('posts.edit')->with('post', $post);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id,Request $request){

          $section_name = $request->input('section_name');

        DB ::table('sections')->where('id',$id)->update(['section_name'=>$section_name]);
        return redirect('admin');
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id){
        DB :: table('sections')->where('id',$id)->delete();
        return redirect('admin');
    }

     public function admin()
    {

        $sections = DB ::table('sections')->get();
        return view('libraryViewsContainer.admin',['sections'=>$sections]);
    }

 }

【问题讨论】:

  • Route::get('/blog/create', 'BlogPostController@create');Route::get('/blog/article/{id}', 'BlogPostController@show');只是给你一个想法

标签: php laravel


【解决方案1】:

不完全确定问题,但您在 routes.php 中的 web 组下列出了路由(因此它应用默认检查)。

当您拥有资源时,它们将使用 CRUD 操作(创建、读取、更新、删除)并将对应于类中的默认操作。否则,创建自己的函数名称,并放置单独的路由。

Route::group(['middleware' => ['web', 'auth']], function () {

    Route::resource('/user', 'UserController');

}

另一种选择是直接在您的路线中调用该方法:

    Route::get('/auth/login', '\App\Http\Controllers\Auth\AuthController@getLogin');
    Route::any('/datafeed/{id}/validate', 'DataFeedController@validateQuery');

您会注意到 {id} 是您选择的函数中可用的变量,即函数 validateQuery($id);

这是一个完整的例子:

            class UserController extends BaseController
            {

                public function __construct(User $model)
                {
                    parent::__construct($model);
                }


                /**
                 * Display a listing of the resource.
                 *
                 * @return \Illuminate\Http\Response
                 */
                public function index()
                {
                    $collection = $this->model->all();
                    return view('user.index')->with('collection', $collection);
                }

                /**
                 * Show the form for creating a new resource.
                 *
                 * @return \Illuminate\Http\Response
                 */
                public function create()
                {
                    return view('user.create');
                }

                /**
                 * Store a newly created resource in storage.
                 *
                 * @param  \Illuminate\Http\Request  $request
                 * @return \Illuminate\Http\Response
                 */
                public function store(Request $request)
                {
                    $input = Input::except(['_method', '_token']);
                    $connector = $this->model->create($input);
                    return redirect()->action('UserController@show', [$connector->id]);
                }

                /**
                 * Display the specified resource.
                 *
                 * @param  int  $id
                 * @return \Illuminate\Http\Response
                 */
                public function show($id)
                {
                    $connector = $this->model->findOrFail($id);
                    return view('user.show')->with('connector', $connector);
                }

                /**
                 * Update the specified resource in storage.
                 *
                 * @param  \Illuminate\Http\Request  $request
                 * @param  int  $id
                 * @return \Illuminate\Http\Response
                 */
                public function edit($id)
                {
                    $connector = $this->model->findOrFail($id);
                    return view('user.edit')->with('connector', $connector);
                }



                /**
                 * Show the form for editing the specified resource.
                 *
                 * @param  int  $id
                 * @return \Illuminate\Http\Response
                 */
                public function update(Request $request, $id)
                {

                    $input = Input::except(['_method', '_token']);
                    $connector = $this->model->findOrFail($id);
                    $connector->update($input);
                    return redirect()->action('UserController@show', [$id]);

                }


                /**
                 * Remove the specified resource from storage.
                 *
                 * @param  int  $id
                 * @return \Illuminate\Http\Response
                 */
                public function destroy($id)
                {
                    $currentID = Auth::user();
                    $user = $this->model->findOrFail($id);

                    if ($currentID->id != $user->id) {
                        $user->delete();
                    } else {
                        Session::flash('flash_message', "You cant delete your own account!");
                        Session::flash('flash_type', 'alert-danger');
                    }

                    return redirect()->action('UserController@index');
                }

还有一个自定义路由的例子:

    Route::any('/datafeed/{id}/execute', 'DataFeedController@execute');


        public function execute($id, $test = false) {
            $results = $this->executeQuery($id, $test);
            return view('datafeed.execute')->with('results', $results);

        }

【讨论】:

  • 我想使用destroy函数但我不知道如何在路由中调用它注意:函数destroy我想在管理页面中使用它
  •  {!! Form::open([ 'method' =&gt; 'DELETE', 'route' =&gt; ['datafeed.destroy', $feed-&gt;id], 'class' =&gt; 'no_float no_padding', ]) !!} { !! Form::submit('Delete', ['class' =&gt; 'btn btn-danger']) !!} {!! Form::close() !!} 
【解决方案2】:

我不完全确定您的计划(或者即使您已完全阅读文档?)但您可以通过在您的 routes.phpRoutes\web.php 中执行类似以下操作来访问这些功能(取决于你的版本)文件:

Route::get('/blog/create', 'BlogPostController@create');

Route::get('/blog/article/{id}', 'BlogPostController@show');

第一部分是定义路由,第二部分是当这个路由在地址栏中匹配时,应该在定义的控制器上运行什么方法。不过,它并不总是必须是 get 请求,您可以使用 getpostpatchput

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多