Unit 3: Controllers, Blade and Advanced Routing - Practice Quiz

INT221 — Mvc Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which Artisan command is used to generate a basic controller class in a Laravel/MVC environment?

A. php artisan new:controller UserController
B. php artisan make:controller UserController
C. php artisan create:controller UserController
D. php artisan generate:controller UserController

2 In a controller method, what is the primary mechanism for accessing data submitted via an HTTP POST request?

A. Using the $_POST superglobal directly
B. Reading from php://input
C. Type-hinting the Illuminate\Http\Request class
D. Using the Input::get() facade only

3 Which method is automatically called when a controller is instantiated, often used to register middleware?

A. init()
B. register()
C. boot()
D. __construct()

4 What is a Single Action Controller?

A. A controller that cannot use middleware
B. A controller that does not extend the base Controller class
C. A controller that can only handle GET requests
D. A controller containing only an __invoke method

5 When generating a Resource Controller, which flag is added to the artisan command?

A. --restful
B. --resource
C. --crud
D. --all

6 Which of the following methods is NOT included in an API Resource Controller (generated with --api)?

A. index
B. edit
C. destroy
D. store

7 Which routing statement creates routes for all seven standard resource actions at once?

A. Route::all('photos', PhotoController::class);
B. Route::crud('photos', PhotoController::class);
C. Route::resource('photos', PhotoController::class);
D. Route::controller('photos', PhotoController::class);

8 In the context of Controller Routing, what does the syntax [UserController::class, 'show'] represent?

A. The controller class and the specific method to execute
B. A resource definition
C. A route group definition
D. An array of middleware to apply

9 What file extension is strictly required for Blade templates?

A. .php
B. .blade
C. .tpl.php
D. .blade.php

10 Which Blade syntax is used to echo data while automatically escaping special characters to prevent XSS attacks?

A. {!! $data !!}
B. @echo($data)
C. <?= $data ?>
D. {{ $data }}

11 If you need to display unescaped HTML data in Blade, which syntax should be used?

A. @raw($data)
B. {!! $data !!}
C. {{ $data }}
D. {{-- $data --}}

12 Which directive is used to define a placeholder in a layout file that child views can inject content into?

A. @extends
B. @yield
C. @include
D. @section

13 Which directive is used at the beginning of a child view to specify which layout it inherits from?

A. @parent
B. @extends
C. @layout
D. @template

14 What is the correct syntax for a Blade comment that will not be rendered in the HTML source?

A. <!-- Comment -->
B. {{-- Comment --}}
C. / Comment /
D. // Comment

15 Inside a @foreach loop in Blade, what special variable is automatically available to check the current iteration state?

A. $foreach
B. $loop
C. $index
D. $iterator

16 Which Blade directive functions as the inverse of @if (executes if the condition is false)?

A. @empty
B. @ifnot
C. @else
D. @unless

17 How do you include a sub-view partials/header.blade.php inside another view?

A. @import('partials.header')
B. @require('partials.header')
C. @include('partials.header')
D. @yield('partials.header')

18 Which directive allows you to define a block of JavaScript that will remain untouched by the Blade engine (useful for JS frameworks)?

A. @verbatim
B. @raw
C. @literal
D. @javascript

19 When defining content for a section, what directive is used to append content to the parent's section rather than overwriting it?

A. @base
B. @super
C. @append
D. @parent

20 What is the primary benefit of using Named Routes?

A. They allow URL generation independent of the actual URI path.
B. They automatically apply security headers.
C. They make the URL shorter.
D. They are processed faster by the server.

21 How do you assign a name to a route definition?

A. Route::get('/path')->label('name');
B. Route::get('/path')->as('name');
C. Route::get('/path')->name('name');
D. Route::get('/path', ['name' => 'name']);

22 What is the correct syntax to define a route parameter constraint ensuring the id is numeric?

A. Route::get('/user/{id}')->where('id', '[0-9]+');
B. Route::get('/user/{id}')->validate('id', 'numeric');
C. Route::get('/user/{id}')->expect('id', '\d+');
D. Route::get('/user/{id}')->constraint('id', 'int');

23 Which method is used to define a group of routes that share common attributes like middleware or prefixes?

A. Route::bundle(...)
B. Route::batch(...)
C. Route::collection(...)
D. Route::group(...)

24 Consider the code: Route::prefix('admin')->group(...). If a route inside the group is /dashboard, what is the resulting URL?

A. /dashboard/admin
B. /admin-dashboard
C. /dashboard
D. /admin/dashboard

25 How do you define an optional parameter in a route?

A. Route::get('/user/[id]')
B. Route::get('/user/*id')
C. Route::get('/user/{id_opt}')
D. Route::get('/user/{id?}')

26 Which feature allows you to restrict a group of routes to a specific subdomain?

A. Route::url('api.example.com')
B. Route::subdomain('api')
C. Route::host('api.example.com')
D. Route::domain('api.example.com')

27 What helper function returns the full URL of the current request?

A. url()->current()
B. Route::current()
C. current_url()
D. url()->now()

28 How do you generate a URL to a specific application route named 'profile'?

A. href('profile')
B. link('profile')
C. url('profile')
D. route('profile')

29 When generating a URL to a named route that requires parameters, how are they passed?

A. route('profile?id=1')
B. route('profile', $id)
C. route('profile', ['id' => 1])
D. route('profile')->with('id', 1)

30 Which helper function is used to generate a URL for a file located in the public directory (like CSS or images)?

A. file()
B. asset()
C. resource()
D. public()

31 What is the purpose of Route::fallback()?

A. To handle database connection failures.
B. To provide a backup controller for heavy loads.
C. To redirect to the home page on error.
D. To define a route that is executed when no other route matches the incoming request.

32 To force a generated URL to use HTTPS, which helper function can be used?

A. https_url()
B. secure_url()
C. url()->secure()
D. ssl_route()

33 Which HTTP verb spoofing field is required in HTML forms to perform PUT or DELETE requests?

A. _token
B. _action
C. _method
D. _verb

34 What does the shortcut Route::view('/welcome', 'pages.welcome'); do?

A. It redirects /welcome to pages.welcome.
B. It creates a view file.
C. It directly returns the view pages.welcome when /welcome is visited, without needing a dedicated controller.
D. It creates a controller that returns a view.

35 What is the result of url()->previous()?

A. NULL if the user is not logged in.
B. The parent directory of the current URL.
C. The URL the user was at before the current request.
D. The URL of the home page.

36 Which directive can be used in Blade to check if the user is authenticated?

A. @user
B. @if(logged_in)
C. @check
D. @auth

37 In a Resource Controller, which method corresponds to the PUT/PATCH HTTP verb?

A. store
B. save
C. update
D. edit

38 What is Route Model Binding?

A. Automatically injecting model instances into routes based on parameter IDs.
B. Binding a route to a specific database table manually.
C. Restricting routes to specific models.
D. Creating routes directly inside Model classes.

39 If you want to define a route that redirects to another URI, which shortcut is most efficient?

A. Route::redirect('/here', '/there');
B. Route::move('/here', '/there');
C. Route::forward('/here', '/there');
D. Route::get('/here', function() { return redirect('/there'); });

40 In Blade, how can you output a variable as JSON, useful for passing data to JavaScript?

A. {{ json_encode($data) }}
B. {{ $data->toJson() }}
C. @json($data)
D. @js($data)

41 What is the purpose of the action() helper?

A. To create a form action attribute.
B. To generate a URL to a given controller action.
C. To perform a controller action immediately.
D. To validate a request action.

42 When using Route::group(['middleware' => ['auth']], ...) what happens?

A. It defines a middleware controller.
B. The middleware is ignored.
C. The routes in the group are public.
D. The auth middleware is applied to all routes defined within the group.

43 Which Blade control structure is used to check if a collection is empty before iterating?

A. @switch ... @case ... @endswitch
B. @while ... @empty ... @endwhile
C. @foreach ... @else ... @endforeach
D. @forelse ... @empty ... @endforelse

44 In a Controller, what does the helper view('home', ['name' => 'John']) do?

A. Compiles the 'home' view and passes the variable $name with value 'John' to it.
B. Redirects to the home route.
C. Creates a new view file named home.
D. Returns a JSON response.

45 How do you define a global pattern constraint for route parameters (e.g., ensuring id is always numeric across all routes)?

A. In the web.php file using Route::global()
B. In the .env file
C. In the Controller's constructor
D. In the RouteServiceProvider's boot method using Route::pattern()

46 Which syntax is used to stop the execution of a Blade @php block?

A. @end
B. @done
C. @stop
D. @endphp

47 If you have a named route photos.index which points to /photos, what does route('photos.index') return?

B. The string '/photos'
C. The controller method
D. The view file path

48 Which parameter constraint regex matches any alphanumeric character?

A. [a-zA-Z0-9]+
B. \w
C. [0-9]+
D. [a-z]+

49 In a Resource Controller, which method corresponds to the POST HTTP verb for creating a new item?

A. store
B. insert
C. make
D. create

50 What is the purpose of the Blade @stack directive?

A. To debug the call stack.
B. To render content pushed to a named stack from other views.
C. To stack elements vertically in CSS.
D. To create a database transaction stack.