Asked  1 Year ago    Answers:  5   Viewed   31 times

I have written a middleware so that the user can't go again in log in page after logging in. Which will redirect to the admin panel if already logged in.

Here is the code:

class RevalidateBackHistory
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
             ->header('Pragma','no-cache')
             ->header('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
    }

}

i have called it inside a controller called NoticeController

 public function __construct()
        {
            $this->middleware('auth');
            $this->middleware('revalidate'); //this is the middleware
        }

I have also defined a function inside this controller to download a file and the code is

public function downloadFile($id)
    {
        $notice = new Notice();
        $data = $notice->where('id',$id)->first();

        if (file_exists(public_path().'/uploads/files/'.$data->file))
        {
            return response()->download(public_path().'/uploads/files/'.$data->file);
        }
        else
        {
            Session()->flash('message.notice',"File not found");
            return redirect('admin/notice/info');
        }

    }

The download function is perfect , i have used this function in another controller also. But the problem is occurring inside this controller when i call downloadFile() function it gives follwing exception.

(1/1) FatalThrowableError

Call to undefined method SymfonyComponentHttpFoundationBinaryFileResponse::header() in RevalidateBackHistory.php (line 20) at RevalidateBackHistory->handle(object(Request), object(Closure))in Pipeline.php (line 148)

If i remove the revalidate middleware from constructor the function works fine. what could be the solution of this problem?

 Answers

2

You should edit your handle function with the following code.

$response = $next($request);
$headers = [
    'Cache-Control' => 'nocache, no-store, max-age=0, must-revalidate',
    'Pragma','no-cache',
    'Expires','Fri, 01 Jan 1990 00:00:00 GMT',
];

foreach($headers as $key => $value) {
    $response->headers->set($key, $value);
}

return $response;

For setting the headers in your RevalidateBackHistory middleware file

Thursday, April 1, 2021
 
Sidarta
 
1
function __construct(){   $ingObject = new Ingredient();   }

ought to be

function __construct(){   $this->ingObject = new Ingredient();   }

In the first case you're setting a local variable, not a field, so it remains null. Then on the validateData you invoke a method on a null variable.

I'm assuming you snipped some code, because your Ingredient class doesn't make sense (there's a $validate variable there that isn't defined).

Thursday, April 1, 2021
 
Alix
 
2

Isn't the point of TDD that your testsuite fails while writing tests?

I guess your point is that it dies with a fatal error instead of just display the red "i failed" bar. Quite an interesting point, I'm doing TDD with phpunit but that never has bugged me at all.

The first thing that came do mind was --process-isolation.

Example:

Let's assume a test class that looks like this:

<?php

class fooTest extends PHPUnit_Framework_TestCase {

    public function testA() {
        $x = new a();
    }

    public function testB() {
        $this->assertTrue(true);
    }
}

using the normal runner phpunit test.php:

PHPUnit 3.5.12 by Sebastian Bergmann.

Fatal error: Class 'a' not found in /home/mcsvnls/mep.php on line 6

but when using the phpunit --process-isolation test.php switch it looks like this:

PHPUnit 3.5.12 by Sebastian Bergmann.

E.

Time: 1 second, Memory: 3.25Mb

There was 1 error:

1) fooTest::testA
RuntimeException: Fatal error: Class 'a' not found in /home/foo/mep.php on line 6

Call Stack:
    0.0005     102364   1. {main}() /home/foo/-:0
    0.0341    1768644   2. __phpunit_run_isolated_test() /home/foo/-:143
    [...........]

FAILURES!
Tests: 2, Assertions: 1, Errors: 1.

And now the second test gets executed and passes

Thursday, April 1, 2021
 
5

Re reading the docs:

Markdown mailables support Blade templates. Blade support only ordinary html and blade syntax.

Individual components can use markdown compilers to parse some or all inputs. That's how @component('mail::table') works.

Update: Markdown versions are used to generate plain text mails. Where as blade templates are used to generate html versions. Again, markdown can be compiled explicitly in blade templates, but that's the extend of it's support.

Saturday, May 29, 2021
 
jwegner
 
2

I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.

What you should do is creating one extra file, for example appHelpershelpers.php and in your composer.json file put:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files": ["app/Helpers/helpers.php"] // <- this line was added
},

create app/Helpers/helpers.php file and run

composer dump-autoload

Now in your app/Helpers/helpers.php file you could add those custom functions for example like this:

if (! function_exists('fooBar')) {
   function fooBar() 
   {
      return AppHelpersCustomHelper::fooBar();
   }
}

so you define global functions but in fact all of them might use specific public methods from some classes.

By the way this is exactly what Laravel does for its own helpers for example:

if (! function_exists('array_add')) {
    function array_add($array, $key, $value)
    {
        return Arr::add($array, $key, $value);
    }
}

as you see array_add is only shorter (or maybe less verbose) way of writing Arr::add

Thursday, August 5, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :