rory's avatar
Level 1

Throw new MethodNotAllowedHttpException($others);

I'm trying to upload a csv file to a form read it and store in a database. But I'm getting an empty error message that refers to: /var/www/tcc/src/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php on the line 255: throw new MethodNotAllowedHttpException($others). Where is my problem?

These are my routes:

Route::get('/', 'DataController@overview');
Route::get('/scrape', 'DataController@scrape');
Route::get('/upload','DataController@upload');
Route::post('/readxsl', ['DataController@read_xsl']);

This is my blade:


<html lang="en">
    <head>
        <title>File Upload</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    </head>
<body>

    <div class="container">
    <h3 class="jumbotron">File Upload</h3>

        <form method="post"  action="{{ action('DataController@read_xsl') }}"  enctype="multipart/form-data">
            {{csrf_field()}}

            <div class="input-group control-group increment" >
                <input type="file" name="filename[]" class="form-control">
            <div class="input-group-btn">
                <button type="button" class="btn btn-info"><i class="glyphicon glyphicon-plus"></i>Add</button>
            </div>
            </div>

            <button type="submit" class="btn btn-primary" style="margin-top:10px">Upload</button>

        </form>
    </div>

</body>
</html>

And this is my controller:

<?php

namespace App\Http\Controllers;
#use GuzzleHttp\Client;
use Goutte\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;;

class DataController extends Controller
{
    public function overview()
    {
        $projects = \DB::table('projects')->get();
        $jobs = \DB::table('jobs')->select('jobs.label', 'jobs.status', 'projects.name')->join('projects', 'projects.id','=','project_id')->get();
        $urls = \DB::table('job_urls')->select('job_urls.url', 'job_urls.hash')->join('jobs', 'jobs.id', '=', 'job_id')->get();
        $contents = \DB::table('job_contents')->select('job_contents.title', 'job_contents.description')->join('job_urls', 'job_urls.id', '=', 'url_id')->get();

        return view('overview', ['projects' => $projects, 'jobs' => $jobs , 'urls' => $urls , 'contents' => $contents]);
    }

// read the urls and extract useful information than load it in the database table
    public function scrape()
    {
        $client = new Client();
        $job_urls = \DB::table('job_urls')->select('job_urls.id', 'job_urls.url')->get();
        #var_dump($job_urls);exit();

        foreach ($job_urls as $elem)
        {
            $url = $elem->url;

            $crawler = $client->request('GET', $url);
            $response = $client->getResponse();

            $stat = $response->getStatus();
            $status = strval($stat); #convert integer to string
            #echo  'STATUS: ', $status;
            #echo gettype($status). "<br />";

            $headers = $response->getHeaders();
            #$headers = json_encode($headers);
            echo 'HEADERS: ';
            #echo gettype($headers) . "<br />";
            /*foreach ($headers as $name => $values){
                echo $name . ': ' . implode(', ', $values) . "\r\n" . "<br />";
            }*/

            $title = '';
            foreach ($crawler->filter('title') as $node)
            {
                $title .= $node->nodeValue;
            }
            #echo 'TITLE: ' , $title . "<br />";

            $metades = $crawler->filterXpath('//meta[@name="description"]')-> attr('content');
            #echo 'METADESCRPTION: ', $metades . "<br />";

            $html = htmlentities($response->getContent());

            $url_id = $elem->id;

            \DB::table('job_contents')->insert(
            [
                'url_id' => $url_id,
                'title' => $title,
                'description' => $metades,
                'header-response' => json_encode($headers),
                'html-page-content'=> $html,
                'status-code'=>$status,
                'created_at'=> \Carbon\Carbon::now(),
            ]);
        }

    }
//file upload form
    public function upload()
    {
        return view('form');
    }

//import data, store in the database and return the view with database table
    public function read_xsl(Request $request)
    {
        $validation = $request->validate([
            'file' => 'required|file|mimes:csv'
        ]);
        $file = $validation['file'];
        $filename = 'uploaded-csv' . time() . '.' . $file->getClientOriginalExtension();
        $path = $file->storeAs('file',$filename);
        dd($path);

        #$path = '../data/blog-urls-gartenbau.csv';
        if (($handle = fopen($path, "r")) !== FALSE)
        {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
            {
                $num = count($data);
                for ($c=0; $c < $num; $c++)
                {
                    $rawUrl = $data[$c];
                    $hashedUrl = sha1($data[$c]);
                    \DB::table('job_urls')->insert(
                        [
                            'job_id' => 1,
                            'url' => $rawUrl,
                            'hash' => $hashedUrl,
                            'created_at' =>  \Carbon\Carbon::now(),
                            'updated_at' => \Carbon\Carbon::now(),
                        ]);
                }
            }
            fclose($handle);
        }
            #return view('form');
    }

0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Why are you passing the controller reference in the last route as an array?

Route::post('/readxsl', ['DataController@read_xsl']);
1 like

Please or to participate in this conversation.