Why can I not use a body class selector with Laravel Dusk? - laravel

I am using Laravel 5.5 and Dusk 2.0. I have the following html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body class="my-body-class" id="my-body-div">
<div class="my-content-class" id="my-content-div">
Content goes here.
</div>
</body>
</html>
Here's my Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test/admin-fixed-layout');
$this->assertNotNull($browser->element('.my-content-class'));
$this->assertNotNull($browser->element('#my-content-div'));
// $this->assertNotNull($browser->element('.my-body-class'));
$this->assertNotNull($browser->element('#my-body-div'));
});
}
If I un-comment the assertion that uses the body class selector, the test fails. Why?

This is because by default prefix is set to body:
public function __construct($driver, $prefix = 'body')
{
$this->driver = $driver;
$this->prefix = trim($prefix);
}
in Laravel\Dusk\ElementResolver class.
If you really need to change this (but probably there is no point), you can add the following method into Tests/DuskTestCase class:
protected function newBrowser($driver)
{
return new \Laravel\Dusk\Browser($driver, new \Laravel\Dusk\ElementResolver($driver, ''));
}
This will override default browser and pass empty prefix instead of default body prefix

Related

How to inject a JS script in Qute Template Engine

I am using Quarkus with the qute template engine. I need to inject some dynamic js script to load on the HTML page. But qute convert js like this:
Template file hello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>Hi {name}!</p>
<script>
{script}
</script>
</body>
</html>
Controller
#RequestScoped
public class Resource {
#Inject
#Location("hello.html")
Template hello;
#Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<String> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", s)
.renderAsync());
}
}
The template render file like below and script will not run on browser:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>NOT_FOUND!</p>
<script>
console.log('test redirect by script')
</script>
</body>
</html>
How can I pass script data to qute template file?
You will want to turn off Character Escapes:
Either use raw:
<script>
{script.raw}
</script>
</body>
</html>
Or return a RawString:
#Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<RawString> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", new RawString(s))
.renderAsync());
}
}

Having trouble getting Dropzone to work with Laravel

Using Laravel Framework 6.18.0
I'm sure this is something simple but i can't seem to figure it out. I don't see the function in the controller being called to actually do the work of moving the file to the server. I've attached a link to the picture below as dropzone seems to be doing its job but the route/controller side seems to be failing.
My routes:
Route::get('/uploadpics', 'UploadPicController#index');
Route::post('/upload','UploadPicController#uploadFiles');
uploadpics.blade.php:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{{asset('js/plugins/dropzone/dist/min/dropzone.min.css')}}">
<script src="{{asset('js/plugins/dropzone/dropzone.min.js')}}" type="text/javascript"></script>
</head>
<body>
<div class="content">
<form method="post" action="{{url('/upload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
#csrf
</form>
</div>
<script>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone",{
maxFilesize: 3, // 3 mb
acceptedFiles: ".jpeg,.jpg,.png",
});
</script>
</body>
</html>
My UploadPicController.php
<?php
namespace App\Http\Controllers;
use App\Coin;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Carbon\Carbon;
class UploadPicController extends Controller
{
public function index(){
return view('pages.uploadpics');
}
public function uploadFiles(Request $request){
dd("i'm in upload"); //**I never get here...**
if($request->hasFile('file')) {
// Upload path
$destinationPath = 'images/';
// Create directory if not exists
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
// Get file extension
$extension = $request->file('file')->getClientOriginalExtension();
// Valid extensions
$validextensions = array("jpeg","jpg","png");
// Check extension
if(in_array(strtolower($extension), $validextensions)){
// Rename file
$fileName = str_slug(Carbon::now()->toDayDateTimeString()).rand(11111, 99999) .'.' . $extension;
// Uploading file to given path
$request->file('file')->move($destinationPath, $fileName);
}
}
}
}
I can use dropzone on the page with no errors but it does not copy the file to the server-- it's like the function in the controller never gets called (see the dd(); - never hits).
enter image description here

Codeigniter's controller data always shows on top of the html

I am trying to get the data of another function inside the controller, but there is a problem when I load it with the view. Anyway I placed the output of the controller inside the view and it still goes to the top of the HTML
this is the controller code
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}//end index()
public function samples(){
echo "string";
}
this is the view
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $record;?>
<h1>This is a sample</h1>
</body>
</html>
Now when I go to the browser it looks ok, but when you go to the source code you can see the data is on top of HTML. Why is that?
source code from view
You should return it
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}
public function samples(){
return "string";
}

Laravel 4 pages

I have a question. How could I create subpages (something like this: character.php?name=Xar) but I want it in Laravel. Do I have to create routes? Also to mention, when I create a route like this:
Route::get('account/test', 'HomeController#test');
and the view is in folder under views/aac/test, and the function is like:
public function test()
{
return View::make('aac.test');
}
it won't load the CSS. it's just an HTML page.
back to the problem again, how could I create sites like that? I'm also using Blade templating engine.
// app/routes.php
Route::get('characters', 'CharactersController#all');
Route::get('characters/{name}', 'CharactersController#detail');
// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
public function all()
{
// show all characters
}
public function detail($name)
{
// find character by name & show detail for example
return View::make('acc.test');
}
}
// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
{{ HTML::style('css/style.css') }}
</head>
<body>
</body>
</html>
Search function
Place search form somewhere in your view file
<form action="{{ URL::action('CharactersController#search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">
</form>
As specified, search form is submited to CharactersController and its search method.
Controller's method
public function search()
{
$name = Inpute::get('search-term');
$searchResult = Character::where('name', '=', $name)->get();
....
}
Register new route
Route::get('characters/search', 'CharactersController#search');

Laravel 4 content yielded before layout

I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>

Resources